我需要按升序从MySQL表中获取数据,但零值排在最后和随机
现在这是我按条件订购的订单。这是行不通的
ORDER BY sortorder=0 RAND(),sortorder
使用条件排序
select *
from table
order by column > 0 desc, column asc, rand()
在末尾添加 rand((
演示
或者你可以使用union
(select * from table where column > 0 order by column asc)
union all
(select * from table where column = 0 order by rand())
演示
如果你想从mysql随机获取数据,零值排在最后。您可能需要尝试以下操作:
SELECT * FROM table ORDER BY `column` = 0, rand();