Not ASC
or DESC
.... Order by custom...
I have tried using case
but not successfully
SELECT * FROM Customers ORDER BY case country when 'P' then 1 …
This is what I want:
By default SQL ORDER BY sort, the column in ascending order but when the descending order is needed ORDER BY DESC can be used. In case when we need a custom sort then we need to use a CASE statement where we have to mention the priorities to get the column sorted.
SELECT `name` FROM `students` WHERE `marks` > 75 ORDER BY SUBSTR(`name`, -3), ID ASC; SUBSTR(name, -3) will select the last three characters in the name column of the student table.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
SELECT * FROM Customers ORDER BY case when country = 'P' then 1 when country = 'A' then 2 when country = 'L' then 3 when country = 'H' then 4 else 5 end asc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With