首先,这是我的数据库的样子:
当前 - 表格
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”列。我只是想不出怎么做。
答案 0 :(得分:2)
max(case when h.status in ('On-Login-Screen','IE-Window-Missing') then h.time end)
或者我猜你也可以写它
max(if(h.status in ('On-Login-Screen','IE-Window-Missing'), h.time, NULL))