我知道,可問題是this重複,但是當我試圖解答他們提供我得到了不同的答案......獲得最接近的日期爲當前日期SQL
,所以我有一個表,今天是06/02/2017
tblpres
patID pres presDate
1 | asd | 05/29/2017
2 | qwe | 05/20/2017
3 | zxc | 06/01/2017
4 | ety | 05/27/2017
5 | ttt | 05/18/2017
,所以我做了一個查詢來獲取最接近的日期爲當前日期,而是我得到ID 3界河是最接近的日期爲當前日期,我收到了差異答案..
SELECT pres,presDate
FROM tblpres
WHERE patID = '20150518384'
ORDER BY presDate DESC
LIMIT 1
SELECT pres, presDate
FROM tblpres
WHERE patID = '20150518384'
ORDER BY ABS(DATEDIFF(presDate, `06/02/2017`))
LIMIT 1
SELECT pres, presDate
from tblpres
WHERE patID = '20150518384'
order by abs('06/02/2017' - presDate) desc
limit 1
SELECT pres,presDate
FROM tblpres
WHERE `patID` = '20150518384' AND presDate < '06/02/2017'
ORDER BY presDate
LIMIT 1
這是我試過的查詢,但似乎沒有任何工作...我得到這個查詢的答案,但結果不是我所期待..
有趣。我很好奇,如果在實際系統中選擇+,然後按+ max分組將比這更快地工作?因爲複雜性,這兩個給出O(n),而「order by」是一種技術上需要O(nlogn)-ish的排序? – iehrlich
只是存儲日期爲日期 – Strawberry