且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL ORDER BY 两个字段条件

更新时间:2022-12-13 10:29:39

在 MySQL 中比较的结果是 01.所以 Stock >01 如果 true.并且 1 大于 0.所以要么使用

The result of a comparison in MySQL is 0 or 1. So Stock > 0 is 1 if true. And 1 and greather than 0. So either use

ORDER BY Stock = 0 ASC, Price DESC

ORDER BY Stock > 0 DESC, Price DESC

ORDER BY case when Stock > 0
              then 1
              else 2
         end, 
         Price DESC