假设我有一张看起来像这样的表:
+-------+-------+
| data | stuff |
+-------+-------+
| 0.90 | a |
| 0.95 | b |
| 0.98 | c |
| 0.99 | d |
| 1.00 | e |
| 1.01 | f |
| 1.02 | g |
+-------+-------+
我需要最贴近'环绕的行。 (上面和下面)给定的数字。例如,如果用户输入0.96,我需要这两行:
| 0.95 | b |
| 0.98 | c |
如果有完全匹配,我只需要那一行。例如,如果用户输入1,我需要这一行:
| 1.00 | e |
MySQL查询会返回这些特定的行吗?
答案 0 :(得分:6)
将UNION DISTINCT
与两个子查询一起使用:
(select * from `table` where data <= 0.96 order by data desc limit 1)
UNION DISTINCT
(select * from `table` where data >= 0.96 order by data asc limit 1)