I wish to do a select on a table and order the results by a certain keyword or list of keywords. For example I have a table like so:
ID Code 1 Health 2 Freeze 3 Phone 4 Phone 5 Health 6 Hot
so rather than just do a simple Order By asc/desc
I'd like to order by Health, Phone, Freeze, Hot
. Is this possible?
The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
If you want to sort some of the data in ascending order and other data in descending order, then you would have to use the ASC and DESC keywords. SELECT * FROM table ORDER BY column1 ASC, column2 DESC; That is how to use the ORDER BY clause in SQL to sort data in ascending order.
Discussion: If you want to select records from a table but would like to see them sorted according to a given column, you can simply use the ORDER BY clause at the end of a SELECT statement. It doesn't matter how complicated or long your SQL query is— ORDER BY should always be at the end of the command.
Try using this:
select * from table order by FIELD(Code, 'Health', 'Phone', 'Freeze', 'Hot')
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