Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要按升序从 MySQL 表中获取数据,但零值最后随机出现
现在这是我按条件订购的。这不起作用
ORDER BY sortorder=0 RAND(),sortorder
使用条件排序
select * from table order by column > 0 desc, column asc, rand()
在末尾添加 rand()
演示
或者你可以使用union
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();