mysql按固定顺序排列 [英] mysql order by a fixed list
本文介绍了mysql按固定顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于要执行随机化步骤,我将要有一个固定的要订购的商品清单,直到运行查询后我才知道.
I am going to have a fixed list of items to order by that I won't know until I run the query since there is a randomization step.
我想要以下内容:
假设is_launch_set将返回1,3,7,11,但已被随机分配到以下位置:
assume that is_launch_set will return 1,3,7,11 but have been randomized to below:
select * from items where is_launch_set=1 order by id values (3,11,7,1);
关于如何实现这一目标的任何想法?我在想也许是一个find_in_set,但不是很确定.
any ideas on how to achieve this? I was thinking maybe a find_in_set but not really sure.
推荐答案
您可以使用以下任一方法来做到这一点:
You can do that by using either:
ORDER BY FIND_IN_SET(id, '3,11,7,1')
或
ORDER BY FIELD(id, 3, 11, 7, 1)
或
ORDER BY CASE id WHEN 3 THEN 0
WHEN 11 THEN 1
WHEN 7 THEN 2
WHEN 1 THEN 3
ELSE 4
END
这篇关于mysql按固定顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文