Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table column sorting based on NULL Value

I want to sort column2 based on Column1 values. I want split column1 data based on NULL value. The final query result will be displayed on evaluating the values in the column1

   Column1    Column2 
    NULL         100
    NULL          60
    NULL          90
    10            22
    20            40
    05            35
    15            20
    40            10
    30            25
    20            30

Would become:

   Column1     Column2 
    20            40
    05            35
    20            30
    30            25
    10            22
    15            20
    40            10
    NULL         100
    NULL          90
    NULL          60

Thanks
like image 912
Theva Deva Avatar asked Apr 12 '26 18:04

Theva Deva


1 Answers

You can use case in your order by clause like this:

Select * from t
Order by case when column1 is null then 1 else 0 end, column2 desc
like image 141
Gurwinder Singh Avatar answered Apr 15 '26 14:04

Gurwinder Singh