select t2.MeterName,t1.count_day,t1.count_time,t1.equipment_name from increment_run_time t1 left join productequipment t2 on t2.EQPName = t1.equipment_name WHERE t1.count_time BETWEEN '2023-05-03 14:40:27' AND '2023-05-04 14:40:27' ORDER BY count_time; select increment_value,tag_name,count_time,count_day from increment_energy where count_time BETWEEN '2023-05-03 14:39:27' AND '2023-05-04 14:44:27';如何把第查询出的第二个sql数据根据时间段关联到第一个sql
时间: 2024-03-09 07:48:35 浏览: 60
可以使用子查询的方式将第二个 SQL 的结果作为一个临时表,然后将其与第一个 SQL 的结果进行关联。如下所示:
```
SELECT t2.MeterName, t1.count_day, t1.count_time, t1.equipment_name, t3.increment_value, t3.tag_name
FROM increment_run_time t1
LEFT JOIN productequipment t2 ON t2.EQPName = t1.equipment_name
LEFT JOIN (
SELECT increment_value, tag_name, count_time, count_day, equipment_name
FROM increment_energy
WHERE count_time BETWEEN '2023-05-03 14:39:27' AND '2023-05-04 14:44:27'
) t3 ON t1.equipment_name = t3.equipment_name AND t1.count_time = t3.count_time
WHERE t1.count_time BETWEEN '2023-05-03 14:40:27' AND '2023-05-04 14:40:27'
ORDER BY t1.count_time;
```
在这个查询中,子查询将第二个 SQL 的结果作为临时表 t3,并将其与第一个 SQL 的结果关联。同时,使用了 LEFT JOIN 以确保即使第二个 SQL 中没有匹配的记录,也能够返回第一个 SQL 中的记录。
阅读全文