Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data in a certain order using mySql

Tags:

php

mysql

I'm busy writing a PHP app that gets data from a database, but I need to get the data in a certain order.

My Query looks as follows

$sql = "select id,title,type from campaigns where id=$cid order by type";

Now my problem is these are the different types 'GC','MJ','MU','MS','MW','MX','GS' and I want MX to always be select last, thus should always be at the end of the row. So the others shpould firstly be selected and then MX. I hope this makes sense.

like image 702
Elitmiar Avatar asked Jan 26 '10 13:01

Elitmiar


2 Answers

You can sort like this:

ORDER BY IF (type = 'MX', 1, 0), type
like image 51
Greg Avatar answered Oct 21 '22 00:10

Greg


(select id,title,type from campaigns where id=$cid and type <> 'MX' order by type)
UNION
(select id,title,type from campaigns where id=$cid and type ='MX')
like image 45
Yada Avatar answered Oct 21 '22 00:10

Yada