tags:

views:

178

answers:

5

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";
+9  A: 

ORDER BY type, title

You can do something like

ORDER BY type DESC, title ASC

too, if you need to.

Michael Krelin - hacker
You both get upvotes, but the ASC is not necessary.
belgariontheking
Yup. That's why I omitted it from the real reply and only added to explanation.
Michael Krelin - hacker
+11  A: 
SELECT * FROM games ORDER BY type, title
Lukasz Lysik
You both get upvotes, but the ASC is not necessary.
belgariontheking
Yes it is default ASC. If you want you can add Desc for descending order
Neil
+3  A: 

Your order by can take a comma separated list of sorts, similar to your result set. Example:

select * from games order by type, entered_dt DESC, title ASC
shrub34
+1  A: 

You are looking something like this :)

select * FROM games ORDER BY type, title
anishmarokey
+13  A: 

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
Bryan S.