【发布时间】:2019-09-25 15:08:45
【问题描述】:
我有几个表,其中包含 time unix 和 time mez 列用于标识。这些将不断填充新数据(准确地说是每 60 秒)。 我创建了一个表,我们从这些表的数据中将其称为 new_table。现在我想更新这个 new_table。但仅限于比 new_table 的最后一个条目更新的其他表的数据。
所以我不必覆盖new_table 或再次收集从第 1 行开始的所有数据,因为它应该运行多年。
我尝试了一些 INSERT 或 UPDATE,但我不知道如何定义应更新数据的 SELECT 和 WHERE 条件。我对SQL非常缺乏经验。提前致谢!
请参阅以下代码了解我是如何创建 new_table 的:
USE database; /* select database */
CREATE TABLE new_table
AS
SELECT
table1_1min.time_unix, table1_1min.time_mez,
/* add power consumption in kW rounded to two decimal places */
ROUND(table1.P + table2.P + table3.P, 2) AS total
FROM
table1_1min, table2_1min, table3_1min
/* make sure same time stamp is used in every row*/
WHERE
table1_1min.time_unix = table2_1min.time_unix
AND table1_1min.time_unix = table3_1min.time_unix
ORDER BY
table1_1min.time_unix;
【问题讨论】:
-
我假设你在 where 子句中的第二个条件应该与
table3_1min.time_unix比较? -
哦,对不起,我的错。是的,你是对的!
-
Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI
JOIN语法-92 SQL 标准(25 多年前),不鼓励使用它