首先这是我的数据库的样子:
当前-- 表
ID - Unique identifier for device (Int)
Location -Unique identifier for name of area (VarChar)
Status - The current status of the device (VarChar )
Time - DateTime of when the last connection was made to the device (DateTime)
历史- 表
CID (Sorta unused, just as an AI field to store multiple old bits of data uniquely) (Int)
ID - Unique identifier for device (Int)
Location --Unique identifier for name of area (VarChar)
Status - The current status of the device (VarChar )
Time -- DateTime of when the last connection was made to the device (DateTime)
所以这就是数据库现在的样子我的查询看起来像这样......
查询 1
SELECT c.*,
if(HOUR(TIMEDIFF(NOW(), c.TIME)) >=1, 1, 0) as LatestOlderThanAnHour,
min(h.time) as EarliestTime,
(abs(timestampdiff(HOUR,NOW(),min(TIME)))/count(*)*100) as percentage
FROM Current c
JOIN Historical h on c.ID = h.ID
WHERE c.Location like "MyLocation"
group by c.ID
查询 2
SELECT MAX(h.TIME) AS LastDown
FROM TABLENAME
WHERE h.STATUS IN ('On-Login-Screen','IE-Window-Missing')
最终目标是为此查询返回的每个 ID 添加一个“Last Down”列。我只是不知道该怎么做。