[英]copy specific row from multiple table into another table in sql
I want to copy a specific row from tables temperature, rules and schedule to stats table. 我想将表温度,规则和时间表中的特定行复制到统计表中。
In temperature table, I want the latest temperature which is 18.6 在温度表中,我想要最新的温度是18.6
mysql> SELECT * FROM currenttemp ORDER BY `timestamp` DESC limit 10 ;
+---------------------+-----------------+-------------+----------+----------+
| timestamp | sensor | currenttemp | humidity | pressure |
+---------------------+-----------------+-------------+----------+----------+
| 2017-03-25 15:28:03 | sensor-1stFloor | 18.6 | 49.85 | 1021.26 |
| 2017-03-25 15:27:03 | sensor-1stFloor | 18.7 | 49.81 | 1021.26 |
| 2017-03-25 15:26:03 | sensor-1stFloor | 18.8 | 49.82 | 1021.26 |
| 2017-03-25 15:25:03 | sensor-1stFloor | 18.9 | 49.85 | 1021.22 |
| 2017-03-25 15:24:03 | sensor-1stFloor | 18.99 | 49.83 | 1021.21 |
| 2017-03-25 15:23:03 | sensor-1stFloor | 18.61 | 49.85 | 1021.18 |
| 2017-03-25 15:22:02 | sensor-1stFloor | 18.62 | 49.8 | 1021.3 |
| 2017-03-25 15:21:02 | sensor-1stFloor | 18.63 | 49.82 | 1021.39 |
| 2017-03-25 15:20:03 | sensor-1stFloor | 18.61 | 49.82 | 1021.28 |
| 2017-03-25 15:19:03 | sensor-1stFloor | 18.62 | 49.82 | 1021.37 |
+---------------------+-----------------+-------------+----------+----------+
In rules table, I want the targettemp for schedule 4 which is 40 在规则表中,我希望日程表4的目标温度为40
mysql> SELECT * FROM rules limit 10 ;
+----+----------+--------+------------+
| id | schedule | sensor | targettemp |
+----+----------+--------+------------+
| 1 | 4 | 1 | 40 |
| 2 | 5 | 1 | 5 |
+----+----------+--------+------------+
In schedule table, I want the endtime for id 4 which is 10:00:00 在时间表表中,我想要ID 4的结束时间是10:00:00
mysql> SELECT * FROM schedules limit 10 ;
+----+--------------+-----------+--------------+-----------+----------+---------+------------+--------+
| id | friendlyname | dayofweek | pretimestart | timestart | endtime | enabled | targettemp | sensor |
+----+--------------+-----------+--------------+-----------+----------+---------+------------+--------+
| 4 | test | 1111110 | 00:00:00 | 00:00:00 | 10:00:00 | 1 | 30 | 1 |
| 5 | sun | 0000001 | 00:00:00 | 00:00:00 | 20:00:00 | 0 | 0 | |
+----+--------------+-----------+--------------+-----------+----------+---------+------------+--------+
I then want to insert these data to the stats table 然后,我想将这些数据插入stats表
currenttemp which is 18.6 当前温度是18.6
targettemp for schedule 4 which is 40 计划4的targettemp为40
endtime for id 4 which is 10:00:00 ID 4的结束时间为10:00:00
the timestamp is done automatically 时间戳是自动完成的
state will be between ON and OFF 状态将在ON和OFF之间
please look at the 1st row in stats table as example from the data copied from the above tables. 请从上述表格复制的数据中查看stats表格中的第一行作为示例。
mysql> SELECT * FROM stats limit 10 ;
+---------------------+-------------+------------+----------+-------+
| timestamp | currenttemp | targettemp | endtime | state |
+---------------------+-------------+------------+----------+-------+
| 2017-03-25 15:41:46 | 18.6 | 40 | 10:00:00 | OFF |
| 2017-03-19 16:53:05 | 16.83 | 5 | 00:00:00 | OFF |
| 2017-03-19 16:54:14 | 16.83 | 40 | 00:00:00 | ON |
| 2017-03-19 20:04:07 | 16.58 | 40 | 00:00:00 | ON |
| 2017-03-19 20:04:15 | 16.58 | 5 | 00:00:00 | OFF |
| 2017-03-19 20:06:29 | 16.58 | 5 | 00:00:00 | OFF |
| 2017-03-19 20:34:28 | 16.54 | 5 | 00:00:00 | OFF |
| 2017-03-19 20:34:56 | 16.54 | 5 | 00:00:00 | OFF |
| 2017-03-19 20:35:26 | 16.54 | 40 | 00:00:00 | ON |
| 2017-03-19 20:38:05 | 16.54 | 40 | 00:00:00 | ON |
+---------------------+-------------+------------+----------+-------+
I will have 2 queries. 我将有2个查询。 One with the state OFF and one with the state ON.
一种是状态为OFF,另一种是状态为ON。
Your question is unclear because the relationship between temperature
and the other tables is unclear. 您的问题尚不清楚,因为
temperature
与其他表格之间的关系尚不清楚。 Let me assume that there is a join key to schedules
. 让我假设
schedules
有一个连接键。 If so, the query you want would look something like this: 如果是这样,您想要的查询将如下所示:
INSERT INTO stats (currenttemp, targettemp, endtime)
SELECT t.temperature, r.targettemp, s.timeend
FROM schedules s INNER JOIN
rules r
ON s.id = r.schedule INNER JOIN
temperature t
ON t.schedule = s.id
WHERE s.id = 4 AND
t.timestamp = (SELECT MAX(t2.timestamp)
FROM temperature t2
WHERE t2.schedule = t.schedule
);
this seems to work 这似乎有效
INSERT INTO stats (currenttemp,targettemp,endtime,state)
SELECT
temperature,r.targettemp,s.timeend,'OFF'
FROM
schedules s
INNER JOIN rules r
ON s.id = r.schedule
INNER JOIN temperature
WHERE timestamp = (SELECT MAX(timestamp) FROM temperature)
AND s.id = 4
i can see now in my stats table 我现在可以在统计信息表中看到
mysql> SELECT * FROM stats limit 10 ;
+---------------------+-------------+------------+----------+-------+
| timestamp | currenttemp | targettemp | endtime | state |
+---------------------+-------------+------------+----------+-------+
| 2017-04-06 17:58:05 | 19.53 | 40 | 10:00:00 | OFF |
+---------------------+-------------+------------+----------+-------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.