使用插入和递归左连接填充缺失的时间
Fill in the missing hours with insert and recursive left join
我有一个 table 温度计。
有时没有收到消息,信息丢失。
我需要每小时用 NULL 填充缺失的行。
CREATE TABLE temp_total(
id int(6) NOT NULL PRIMARY KEY,
stamp timestamp NOT NULL,
room_temp decimal(3,1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE temp_total
ADD UNIQUE KEY stamp(stamp),
MODIFY id int(6) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
INSERT INTO temp_total(stamp, room_temp) VALUES
('2019-07-21 19:00:00', '23.4'),
('2019-07-21 22:00:00', '22.7'),
('2019-07-23 02:00:00', '22.5'),
('2019-07-23 06:00:00', '22.4');
预期结果是一个 36 行的数组。
我发现这个查询工作正常。
SELECT stamp INTO @deb FROM temp_total ORDER BY stamp ASC LIMIT 1;
SELECT stamp INTO @fin FROM temp_total ORDER BY stamp DESC LIMIT 1;
WITH RECURSIVE all_hours(dt) AS (
SELECT @deb dt
UNION ALL
SELECT dt + INTERVAL 1 HOUR FROM all_hours
WHERE dt + INTERVAL 1 HOUR < @fin + INTERVAL 1 HOUR
)
-- INSERT IGNORE INTO temp_total(stamp, room_temp)
SELECT d.dt stamp, t.room_temp
FROM all_hours d
LEFT JOIN temp_total t ON t.stamp = d.dt
ORDER BY d.dt;
我想将 SELECT 的结果与 INSERT 一起使用,但我收到此消息:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO temp_total(stamp, room_temp)
SELECT d.dt stamp, t.room_temp
...' at line 7
当我取消注释该行时。
你快到了。语法稍作更改,查询将按预期工作:
INSERT IGNORE INTO temp_total(stamp, room_temp)
WITH RECURSIVE all_hours(dt) AS (
SELECT @deb dt
UNION ALL
SELECT dt + INTERVAL 1 HOUR FROM all_hours
WHERE dt + INTERVAL 1 HOUR < @fin + INTERVAL 1 HOUR
)
SELECT d.dt stamp, t.room_temp
FROM all_hours d
LEFT JOIN temp_total t ON t.stamp = d.dt
ORDER BY d.dt;
请参阅 db<>fiddle 中的 运行 示例。
我有一个 table 温度计。 有时没有收到消息,信息丢失。
我需要每小时用 NULL 填充缺失的行。
CREATE TABLE temp_total(
id int(6) NOT NULL PRIMARY KEY,
stamp timestamp NOT NULL,
room_temp decimal(3,1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE temp_total
ADD UNIQUE KEY stamp(stamp),
MODIFY id int(6) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
INSERT INTO temp_total(stamp, room_temp) VALUES
('2019-07-21 19:00:00', '23.4'),
('2019-07-21 22:00:00', '22.7'),
('2019-07-23 02:00:00', '22.5'),
('2019-07-23 06:00:00', '22.4');
预期结果是一个 36 行的数组。
我发现这个查询工作正常。
SELECT stamp INTO @deb FROM temp_total ORDER BY stamp ASC LIMIT 1;
SELECT stamp INTO @fin FROM temp_total ORDER BY stamp DESC LIMIT 1;
WITH RECURSIVE all_hours(dt) AS (
SELECT @deb dt
UNION ALL
SELECT dt + INTERVAL 1 HOUR FROM all_hours
WHERE dt + INTERVAL 1 HOUR < @fin + INTERVAL 1 HOUR
)
-- INSERT IGNORE INTO temp_total(stamp, room_temp)
SELECT d.dt stamp, t.room_temp
FROM all_hours d
LEFT JOIN temp_total t ON t.stamp = d.dt
ORDER BY d.dt;
我想将 SELECT 的结果与 INSERT 一起使用,但我收到此消息:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO temp_total(stamp, room_temp)
SELECT d.dt stamp, t.room_temp
...' at line 7
当我取消注释该行时。
你快到了。语法稍作更改,查询将按预期工作:
INSERT IGNORE INTO temp_total(stamp, room_temp)
WITH RECURSIVE all_hours(dt) AS (
SELECT @deb dt
UNION ALL
SELECT dt + INTERVAL 1 HOUR FROM all_hours
WHERE dt + INTERVAL 1 HOUR < @fin + INTERVAL 1 HOUR
)
SELECT d.dt stamp, t.room_temp
FROM all_hours d
LEFT JOIN temp_total t ON t.stamp = d.dt
ORDER BY d.dt;
请参阅 db<>fiddle 中的 运行 示例。