0

所以我已经达到了我的 sql 能力的绝对极限,我无法终生结合这两个查询,首先这是我的数据库的样子

当前 - 表

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)

综上所述,这里有两个查询:

查询一:

SELECT *, if(HOUR(TIMEDIFF(NOW(), TIME)) >=1, 1, 0) as OlderThanAnHour
FROM Current
WHERE Location like "MyLocation"

查询 2:

SELECT ID, min(time), (abs(timestampdiff(HOUR,NOW(),TIME))/count(*)*100) as percentage
from Historical
where LOCATION like "MyLocation"
group by ID

我的目标是将它们组合成一个查询,因为它会经常被调用

4

1 回答 1

3

尝试:

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
于 2013-04-26T16:14:30.487 回答