Order resultset based on WHERE IN clause data

最后都变了- 提交于 2019-12-05 15:53:47

Use a CASE statement in the ORDER BY:

ORDER BY CASE someColumn
           WHEN value1 THEN 1
           WHEN value2 THEN 2
           WHEN value3 THEN 3
         END ASC

Assign the arbitrary values as you like. I don't normally include ASC in ORDER BY because it is implied if not defined, but I wanted to be explicit in case you want in DESC order.

No, the order is not guaranteed - or rather, it will be the order the selected rows appear in the database.

If you know the values will belong to a strict set you can make the column an ENUM type with the order you want, and sorting on this field will sort by that order.

One simple way to order by certain values is this:

ORDER BY `someColumn`='value1' DESC, `someColumn`='value2' DESC, ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!