将多个表中的特定行复制到sql中的另一个表中

时间:2017-03-25 11:36:08

标签: mysql sql inner-join

我想将表格温度,规则和时间表中的特定行复制到统计表。

在温度表中,我想要最新温度为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 |
+---------------------+-----------------+-------------+----------+----------+

在规则表中,我希望计划4的targettemp为40

mysql> SELECT * FROM rules limit 10 ;
+----+----------+--------+------------+
| id | schedule | sensor | targettemp |
+----+----------+--------+------------+
|  1 |        4 | 1      |         40 |
|  2 |        5 | 1      |          5 |
+----+----------+--------+------------+

在日程表中,我想要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 |        |
+----+--------------+-----------+--------------+-----------+----------+---------+------------+--------+

然后我想将这些数据插入统计表

currenttemp是18.6
计划4的目标计划是40
id 4的终结时间是10:00:00

时间戳自动完成
状态将在ON和OFF之间

请查看stats表中的第1行作为示例,从上表中复制的数据。

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    |
+---------------------+-------------+------------+----------+-------+

我将有2个查询。一个状态为OFF,另一个状态为ON。

2 个答案:

答案 0 :(得分:0)

您的问题不明确,因为No such file or directory @ rb_sysopen - /test/data.json与其他表格之间的关系尚不清楚。我假设有temperature的连接键。如果是这样,您想要的查询将如下所示:

schedules

答案 1 :(得分:0)

这似乎有用

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

我现在可以在我的统计表中看到

mysql> SELECT * FROM stats limit 10 ;
+---------------------+-------------+------------+----------+-------+
| timestamp           | currenttemp | targettemp | endtime  | state |
+---------------------+-------------+------------+----------+-------+
| 2017-04-06 17:58:05 |       19.53 |         40 | 10:00:00 | OFF   |
+---------------------+-------------+------------+----------+-------+