Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ORDER BY based on two different columns

Tags:

sql

mysql

I have a table with 5 columns. When I list the table, I want to order by one column so that the types are grouped together and then order them alphabetical so that they are easy to find.

Is it possible to ORDER by two different columns?

Here is my current select:

$query_rs_cms = "SELECT * FROM games ORDER BY type ASC";

I guess what I'm looking for is something like:

SELECT * 
   FROM games 
ORDER BY type THEN ORDER BY title ASC";
like image 473
krx Avatar asked Sep 15 '09 17:09

krx


2 Answers

You can specify the ORDER BY by either the column name or by the column number of the return.

So you could do:

SELECT * FROM games ORDER BY Type, Title

Or something like:

SELECT Type, Title, Column1, Column2 FROM games ORDER BY 1, 2

You can also do ascending and descending. So if you wanted Type Descending but Title Ascending:

SELECT * FROM games ORDER BY Type DESC, Title ASC
like image 191
Bryan S. Avatar answered Sep 30 '22 04:09

Bryan S.


SELECT * FROM games ORDER BY type, title
like image 28
Lukasz Lysik Avatar answered Sep 30 '22 05:09

Lukasz Lysik