这是我的表测试值:
Price
----------
300
600
900
1000
1800
2000
我想这样查询,当我搜索 300 时,我应该得到 4 条记录 300,600,900,1000。
如果我搜索900,我可以得到600,900,1000,1800。 即两条记录 <=900 和两条记录 >900
这是我试过的查询:
SELECT * FROM table h where CONDITIONS
and (price in (select price from table where price <=900) // I want to add LIMIT 2 in this subquery
or price in (select price from table where price >900)//LIMIT 2
)
order by FIELD(price ,900) DESC limit 5;
我在堆栈溢出方面进行了很多搜索,但没有任何效果。请帮忙。
最佳答案
您可以尝试以下...
select * from ((select h.* from table_name h where amount <=300 order by amount desc limit 2)
union
(select h.* from table_name h where amount >300 order by amount limit 2))
derived_table order by FIELD(amount,300) desc;
关于MySQL 查询在子查询中添加限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28094374/