如何按照订单状态排序 mysql 数据,让“-1”始终排最后,“2”排最前
在 mysql 中,排序查询时按状态对订单表进行排序时,可以根据不同的状态分配特定的排序值,从而实现自定义排序规则。
sql 代码:
select * from ( select case when status == 2 then 7 when status == -1 then -1 else status end as newStatus, status from m_table ) m order by newStatus desc;
解释:
case 语句根据不同的状态值分配新的排序值 newstatus:
这样,待操作状态(status 为 2)始终排在最前面(newstatus 为 7),撤销状态(status 为 -1)始终排在最后(newstatus 为 -1),其他状态按升序排列。
以上就是如何让 MySQL 中的订单按照状态排序,使 “2” 始终排最前,“-1” 排最后?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号