-1

我需要按升序从 MySQL 表中获取数据,但零值最后随机出现

现在这是我按条件订购的。这不起作用

ORDER BY sortorder=0 RAND(),sortorder
4

2 回答 2

2

使用条件排序

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())

演示

于 2018-06-10T11:28:13.780 回答
0

如果您想从 mysql 随机获取数据,并且零值排在最后。您可能想尝试以下方法:

SELECT * FROM table ORDER BY `column` = 0, rand();
于 2018-06-10T11:33:55.633 回答