Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Order By list of strings?

Tags:

sql

mysql

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?

like image 279
Tikeb Avatar asked Aug 07 '09 10:08

Tikeb


People also ask

How are strings ordered in SQL?

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.

How do you SELECT data alphabetically in SQL?

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.

How do I group by alphabetically in SQL?

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.


1 Answers

Try using this:

select * from table  order by FIELD(Code, 'Health', 'Phone', 'Freeze', 'Hot') 
like image 116
shantanuo Avatar answered Sep 29 '22 11:09

shantanuo