-1

I need to get data from MySQL table in ascending order but zero values come last and randomly

Right now this is my order by condition.This is not working

ORDER BY sortorder=0 RAND(),sortorder
Aishu
  • 47
  • 9

2 Answers2

2

Use conditional ordering

select *
from table
order by column > 0 desc, column asc, rand()

Add rand() at the end

Demo

Or you could use union

(select * from table where column > 0 order by column asc)
union all
(select * from table where column = 0 order by rand())

Demo

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
0

If you want to get data from mysql randomly and zero values come last. You may want to try the following:

SELECT * FROM table ORDER BY `column` = 0, rand();
Shoukat Mirza
  • 800
  • 9
  • 19