mysql命令由固定列表组成

时间:2021-06-10 22:47:42

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.

我将有一个固定的项目列表,我将不知道,直到我运行查询,因为有一个随机化步骤。

I would like to have something like the following:

我希望得到以下内容:

assume that is_launch_set will return 1,3,7,11 but have been randomized to below:

假设is_launch_set将返回1,3,7,11但已被随机化到下面:

select * from items where is_launch_set=1 order by id values (3,11,7,1);

any ideas on how to achieve this? I was thinking maybe a find_in_set but not really sure.

关于如何实现这一点的任何想法?我在想也许是一个find_in_set但不太确定。

1 个解决方案

#1


59  

You can do that by using either:

您可以使用以下任一方法来实现:

ORDER BY FIND_IN_SET(id, '3,11,7,1')

or

要么

ORDER BY FIELD(id, 3, 11, 7, 1)

or

要么

ORDER BY CASE id WHEN 3 THEN 0
                WHEN 11 THEN 1
                 WHEN 7 THEN 2
                 WHEN 1 THEN 3
                        ELSE 4
         END

#1


59  

You can do that by using either:

您可以使用以下任一方法来实现:

ORDER BY FIND_IN_SET(id, '3,11,7,1')

or

要么

ORDER BY FIELD(id, 3, 11, 7, 1)

or

要么

ORDER BY CASE id WHEN 3 THEN 0
                WHEN 11 THEN 1
                 WHEN 7 THEN 2
                 WHEN 1 THEN 3
                        ELSE 4
         END