Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL ORDER BY (sequence) [duplicate]

I have a sql statement which I would want to ORDER BY a specific sequence.

SELECT * FROM UserDB ORDER BY [Role]

How can I make it such that the data brought to my GridView table is listed from Admin on the top, follow by User and Guests?

like image 837
gymcode Avatar asked Nov 20 '25 09:11

gymcode


1 Answers

So you want to order by Admin/User/Guest?

Try something like :

SELECT * 
FROM UserDB 
ORDER BY
  CASE Role WHEN 'Admin' THEN 0 
            WHEN 'User' THEN 1
            WHEN 'Guest' THEN 2
  END

Does that work for you?

Another option would be to have (or add) a column Sequence to your Role table so you could define the sequence in the table itself - and then just to an ORDER BY Role.Sequence.

like image 119
marc_s Avatar answered Nov 22 '25 00:11

marc_s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!