用另外两个 table 的两个字段的最大值更新 table 的字段值
Update field value of a table with max value of two fields of two other tables
我有一些数据 tables 具有以下架构
DeviceStatuses Table
id, Last_Comm, Device_Id
00001, 2020-10-23, DEV1
00002, 2020-09-23, DEV2
RcptStatuses Table
id, Last_Comm, Source
R0001, 2020-10-25, DEV1
R0002, 2020-09-25, DEV2
R0003, 2020-10-30, DEV1
ReceivedTrans Table
id, Last_Comm, Source
R0001, 2020-10-25, DEV1
R0002, 2020-09-25, DEV2
R0003, 2020-10-31, DEV1
我需要更新“DeviceStatuses”Table的“Last_Comm”字段值与“RcptStatuses Table的Last_Comm”字段值和“ReceivedTrans Table's Last_Comm”字段值。由于某些限制,我必须使用单个查询来执行此操作。
这些是预期的输出
DeviceStatuses Table (After update)
id, Last_Comm, Device_Id
00001, 2020-10-31, DEV1 (max value for DEV1 Last_Comm from RcptStatus and RecievedTx Table)
00002, 2020-09-25, DEV2 (max value for DEV2 Last_Comm from RcptStatus and RecievedTx Table)
我试过了
UPDATE DeviceStatuses SET Last_Comm =
(SELECT MAX(lst) FROM (SELECT rsns.Last_Commu AS lst FROM RcptStatuses rsns , DeviceStatuses WHERE Device_Id = rsns.Source
UNION ALL
SELECT rtns.Last_Comm AS lst FROM ReceivedTrans rtns, DeviceStatuses WHERE Device_Id = rtns.Source ) As T)
WHERE
(SELECT MAX(lst) FROM (SELECT rsns.Last_Comm AS lst FROM RcptStatuses rsns, DeviceStatuses WHERE Device_Id = rsns.Source
UNION ALL
SELECT rtns.Last_Comm AS lst FROM ReceivedTrans rtns , DeviceStatuses WHERE Device_Id = rtns.Source ) AS T ) > Last_Comm
但这会导致所有设备同时更新(设备 001 的 lastCom)。
其他需要考虑的事项:-
- DeviceId 和 Source 不唯一(可能在 table 中重复)
- 仅当 DeviceStatuses table 的 DeviceId 值小于其他 table 的最大值或 DeviceId 字段值为空时才需要更新 DeviceId
- 数据库驱动程序是 MySQL
知道怎么做吗?
不清楚您要更新哪一列(last_comm 或 device_ID),如果您想要更新 last_comm 对应的 device_id,您可以尝试使用基于更新的加入最大结果
UPDATE DeviceStatuses d
INNER JOIN (
select source, max(Last_Comm ) max_last_comm
from (
select source, Last_Comm
from RcptStatuses
UNION
select source, Last_Comm
from ReceivedTrans
) t
group by source
) t2 ON d.Device_Id = t2.source
SET d.Last_Comm = t2.max_last_comm
您不需要从其他表中获取 MAX(Last_Comm)
。只需加入其他表中的 每一行 ,并将 Last_Comm
提升到其中的最大值。它会逐行执行此操作,但到最后,DeviceStatuses.Last_Comm
将具有最大值。
UPDATE DeviceStatuses AS d
JOIN RcptStatuses AS rs ON d.Device_ID = rs.Source
JOIN ReceivedTrans AS rt ON d.Device_ID = rt.Source
SET d.Last_Comm = GREATEST(d.Last_Comm, rs.Last_Comm, rt.Last_Comm)
但是如果 DeviceStatuses.Device_ID
为 NULL,我不知道您希望如何将它与其他表中的任何行匹配。
我有一些数据 tables 具有以下架构
DeviceStatuses Table
id, Last_Comm, Device_Id
00001, 2020-10-23, DEV1
00002, 2020-09-23, DEV2
RcptStatuses Table
id, Last_Comm, Source
R0001, 2020-10-25, DEV1
R0002, 2020-09-25, DEV2
R0003, 2020-10-30, DEV1
ReceivedTrans Table
id, Last_Comm, Source
R0001, 2020-10-25, DEV1
R0002, 2020-09-25, DEV2
R0003, 2020-10-31, DEV1
我需要更新“DeviceStatuses”Table的“Last_Comm”字段值与“RcptStatuses Table的Last_Comm”字段值和“ReceivedTrans Table's Last_Comm”字段值。由于某些限制,我必须使用单个查询来执行此操作。
这些是预期的输出
DeviceStatuses Table (After update)
id, Last_Comm, Device_Id
00001, 2020-10-31, DEV1 (max value for DEV1 Last_Comm from RcptStatus and RecievedTx Table)
00002, 2020-09-25, DEV2 (max value for DEV2 Last_Comm from RcptStatus and RecievedTx Table)
我试过了
UPDATE DeviceStatuses SET Last_Comm =
(SELECT MAX(lst) FROM (SELECT rsns.Last_Commu AS lst FROM RcptStatuses rsns , DeviceStatuses WHERE Device_Id = rsns.Source
UNION ALL
SELECT rtns.Last_Comm AS lst FROM ReceivedTrans rtns, DeviceStatuses WHERE Device_Id = rtns.Source ) As T)
WHERE
(SELECT MAX(lst) FROM (SELECT rsns.Last_Comm AS lst FROM RcptStatuses rsns, DeviceStatuses WHERE Device_Id = rsns.Source
UNION ALL
SELECT rtns.Last_Comm AS lst FROM ReceivedTrans rtns , DeviceStatuses WHERE Device_Id = rtns.Source ) AS T ) > Last_Comm
但这会导致所有设备同时更新(设备 001 的 lastCom)。
其他需要考虑的事项:-
- DeviceId 和 Source 不唯一(可能在 table 中重复)
- 仅当 DeviceStatuses table 的 DeviceId 值小于其他 table 的最大值或 DeviceId 字段值为空时才需要更新 DeviceId
- 数据库驱动程序是 MySQL
知道怎么做吗?
不清楚您要更新哪一列(last_comm 或 device_ID),如果您想要更新 last_comm 对应的 device_id,您可以尝试使用基于更新的加入最大结果
UPDATE DeviceStatuses d
INNER JOIN (
select source, max(Last_Comm ) max_last_comm
from (
select source, Last_Comm
from RcptStatuses
UNION
select source, Last_Comm
from ReceivedTrans
) t
group by source
) t2 ON d.Device_Id = t2.source
SET d.Last_Comm = t2.max_last_comm
您不需要从其他表中获取 MAX(Last_Comm)
。只需加入其他表中的 每一行 ,并将 Last_Comm
提升到其中的最大值。它会逐行执行此操作,但到最后,DeviceStatuses.Last_Comm
将具有最大值。
UPDATE DeviceStatuses AS d
JOIN RcptStatuses AS rs ON d.Device_ID = rs.Source
JOIN ReceivedTrans AS rt ON d.Device_ID = rt.Source
SET d.Last_Comm = GREATEST(d.Last_Comm, rs.Last_Comm, rt.Last_Comm)
但是如果 DeviceStatuses.Device_ID
为 NULL,我不知道您希望如何将它与其他表中的任何行匹配。