mysql 如何使用基于两个表的事件计划程序?

mwecs4sa  于 2023-01-29  发布在  Mysql
关注(0)|答案(1)|浏览(133)
CREATE EVENT sensor_checker
ON SCHEDULE EVERY 15 MINUTE
DO
UPDATE tb_device d 
inner join arduino_id a 
on d.id_device = a.id_node
SET d.status_device = 0 WHERE ADDTIME(new.date_server, '900') < now()

因此,我有两个表tb_arduino和tb_device,我正在尝试创建一个事件,当列“date_server”(来自tb_arduino表)距离当前时间不到15分钟时,列“status_device”(来自tb_device)将更新列“status_device”的值为0,否则列将更新值为1。但上面的查询似乎不起作用。
更多说明:
如果当前时间为12:20:40

  • “tb_arduino”表格*
| id_node  | luminance | date_server           |
| -------- | --------  | --------              |
| s01      | 270       | 2023-27-01 12:20:30   |
| s01      | 280       | 2023-27-01 12:10:30   |
| s02      | 210       | 2023-27-01 12:00:30   |
| s02      | 230       | 2023-27-01 11:55:30   |

然后,“id_node”s02的“status_device”值变为0

  • “tb设备”表*
| id_device | status_device |
| --------  | --------      |
| s01       | 1             |
| s02       | 0             |
iklwldmw

iklwldmw1#

测试以下内容:

CREATE EVENT sensor_checker
ON SCHEDULE EVERY 15 MINUTE
DO
UPDATE tb_device
SET status_device = EXISTS (
    SELECT NULL
    FROM arduino_id 
    WHERE tb_device.id_device = arduino_id.id_node
      AND arduino_id.date_server >= NOW() - INTERVAL 15 MINUTE
    );

当然,首先要单独测试UPDATE查询。

相关问题