0

首先这是我的数据库的样子:

当前-- 表

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”列。我只是不知道该怎么做。

4

1 回答 1

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))
于 2013-05-07T20:36:49.700 回答