关于按字段中的类型进行排序的sql语句。例(status=1,2,3,4...)

今天项目有个需求说是要,先展示状态为2的表数据,并且其他状态的数据也得展示,但是不排序,而且整体时间倒序。

这个需求听见觉得应该不难,但是真正写起来,怎么都写不对。

故,度娘查到大家给的答案后,才根据需求改了改。

sql语句如下:

 

SELECT * FROM 表
ORDER BY CASE 
WHEN STATUS=2 THEN 1 ----给状态为2的排序,then 1排第一。

(这里可以写多个排序,和上面格式一样。)
ELSE 2                                ----这里可以选不想要排序的status。
 END,create_date DESC     ----这个写时间倒序。

这里注意!!!

ENDcreate_date DESC 

这里一定要加个逗号

附上百度来的链接,供大家参考。

https://blog.csdn.net/weixin_42236021/article/details/80688056

优化这条sql: select distinct (select product_name from t_product from where id = #{productId} and mark = 1 and status = 1) as productName, (select count(0) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status in(1,2,3,31,32,33)) as clueCount, (select count(0) from t_clue a left join t_clue_appendix b on a.clue_code = b.clue_code where a.distribution_status != 4 and a.mark = 1 and b.file_url is not null and a.product_id = #{productId} and a.status in (3,31,32,33)) as intentionCount, (select count(0) from t_clue a where a.status in (4,5,7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status = 4) as incomingCount, (select count(0) from t_clue a where a.status in (5,7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status in (5,7,8)) as approvedCount, (select count(0) from t_clue a where a.status = 6 and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId} and a.status = 6) as rejectionCount, (select count(0) from t_clue a where a.status in (7,8) and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as loanCount, (select count(0) from t_clue a where a.status = 8 and a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as swipeCount, (select sum(a.loan_amount) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as loanMoney, (select sum(a.use_amount) from t_clue a where a.distribution_status != 4 and a.mark = 1 and a.product_id = #{productId}) as swipeMoney
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值