我有下面的查询,它返回一个特定时间间隔的结果集,如1分钟、10分钟、1小时等。[下面是工作的,如10分钟。]
select MemoryUsage, TransactionTime, moduleid
from tbl_trnmemoryusage
where TransactionTime between (now() - interval 10 minute ) and now()
order by TransactionTime asc;
要求
如果上面的查询没有返回任何内容,即在该时间段内没有数据,则返回基于最后一个条目的“TransactionTime”列
if above_query returns null
then
select MemoryUsage, TransactionTime, moduleid
from tbl_trnmemoryusage
order by TransactionTime desc limit 1;
sqlfiddle
最佳答案
SELECT
COALESCE(t.MemoryUsage, t2.MemoryUsage),
COALESCE(t.TransactionTime, t2.TransactionTime),
COALESCE(t.moduleid, t2.moduleid)
FROM (
SELECT *
FROM tbl_trnmemoryusage
ORDER BY TransactionTime DESC
LIMIT 1
) t2
LEFT JOIN (
SELECT *
FROM tbl_trnmemoryusage
WHERE TransactionTime BETWEEN (NOW() - INTERVAL 10 MINUTE) and NOW()
ORDER BY TransactionTime ASC
) t
ON 1
关于mysql - 如果选择查询返回null,则从mysql中的db返回最新条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37258960/