我有一个表,称之为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