今天项目有个需求说是要,先展示状态为2的表数据,并且其他状态的数据也得展示,但是不排序,而且整体时间倒序。
这个需求听见觉得应该不难,但是真正写起来,怎么都写不对。
故,度娘查到大家给的答案后,才根据需求改了改。
sql语句如下:
SELECT * FROM 表
ORDER BY CASE
WHEN STATUS=2 THEN 1 ----给状态为2的排序,then 1排第一。
(这里可以写多个排序,和上面格式一样。)
ELSE 2 ----这里可以选不想要排序的status。
END,create_date DESC ----这个写时间倒序。
这里注意!!!
END,create_date DESC
这里一定要加个逗号。
附上百度来的链接,供大家参考。
https://blog.csdn.net/weixin_42236021/article/details/80688056