我有这个问题:
SELECT `name` FROM `products` WHERE `id` = 0 OR `id` = 4 OR `id` = 2
答案 0 :(得分:3)
试试这个
select name from products WHERE id=0 OR id= 4 OR id=2 order by FIELD(id,0,4,2);
FIELD()返回4,2,...列表中0的索引(位置)。如果未找到0,则返回0.
注意:这会让你的查询慢一点。
答案 1 :(得分:0)
我相信您正在寻找FIND_IN_SET()
功能:
SELECT name FROM products WHERE id IN (0,4,3) ORDER BY FIND_IN_SET(id, '0,4,3');