我有一個表,稱之爲MyTable,它有一列,稱爲「myvalue」。MySQL:如果存在結果大於數字的結果,但如果結果不存在,則結果大於下面的結果?
我需要一個查詢,它需要一個數字並根據與myvalue的比較返回一行。
如果number = myvalue,那麼這是正確的行。如果沒有匹配,那麼我需要把數值剛好超過數字。如果這不存在,我需要採取低於數字的值。
因此,例如,讓我們假設myvalue的列有這些值:
100
200
300
400
500
If I query with number=50 it will return the row with '100'.
If I query with number=100 it will return the row with '100'.
If I query with number=101 it will return the row with '200'.
If I query with number=450 it will return the row with '500'.
If I query with number=570 it will return the row with '500'.
到目前爲止,我有一個查詢,其將返回的匹配值還是一個正上方,但我不知道如何如果上面的那個不存在,就讓它返回下面的那個。
這就是我現在做的事:
SELECT * FROM MyTable WHERE myvalue >= 'number' ORDER BY myvalue ASC LIMIT 1
(當然更換一個價值數 '')
任何想法?我可以想到一種暴力方法,但我確信有一些優雅的東西是我無法想象的。
感謝,走出3個答案到目前爲止,這僅僅是對於那些數量大於表中的最大myvalue的大的情況下工作的人。 – nebs