Here’s how to order your data by two or more columns using PHP and MySQL
Order by Two Columns
Here’s how to order by more than one column. When ordering by more than one column, the second column is only used if the values are identical with the onces in the first column:
SELECT column_name(s)
FROM table_name
ORDER BY column_name1, column_name2
Sort the display by Ascending or Descending Order
If you use the ORDER BY keyword, the sort-order of the recordset is ascending by default (1 before 9 and “a” before “p”).
Use the DESC keyword to specify a descending sort-order (9 before 1 and “p” before “a”):
SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC
Please rate this post by clicking a star:



(21 votes, average: 3.90 out of 5)
Thanks for the tip! Just to add, if you need to sort by two columns, one ascending and the other descending, then here’s how to do it:
SELECT column_name(s) FROM table_name ORDER BY column_name1 DESC, column_name2 ASC
hi, i need to use order by in other directions like : date desc and priority asc
i used this : select * from tablename where category = ‘1′ order by date desc, priority asc limit 30
this is not working right
if use this :
select * from tablename where category = ‘1′ order by priority, date desc limit 30
this is working but i want date desc first then priority asc, how to do this please help
rahul
actually I need first column descending and second column ascending, but its only working if i use first column ascending and second column descending.