我目前有两个select语句来获取最新的行和最接近24小时前的行。在一个语句中选择两者的最优化方法是什么?
我目前正在检索最新的:
SELECT `Price` FROM `".$this->symbol."Trades` ORDER BY `ts` DESC LIMIT 1
最接近24小时前的那个:
SELECT `Price` FROM `".$this->symbol."Trades` WHERE `ts` >= DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY `ts` ASC LIMIT 1
答案 0 :(得分:0)
你对工会的询问
SELECT `Price` FROM `".$this->symbol."Trades` ORDER BY `ts` DESC LIMIT 1
union
SELECT `Price` FROM `".$this->symbol."Trades` WHERE `ts` >= DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY `ts` ASC LIMIT 1
答案 1 :(得分:0)
如果您只是要合并结果,可以使用union
http://dev.mysql.com/doc/refman/5.7/en/union.html
(SELECT `Price` FROM `".$this->symbol."Trades` ORDER BY `ts` DESC LIMIT 1)
UNION
(SELECT `Price` FROM `".$this->symbol."Trades` WHERE `ts` >= DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY `ts` ASC LIMIT 1)