PHP MySQL Order by Two Columns

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

You may also like...

14 Responses

  1. Hasnain says:

    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

  2. Rahul Dev Katarey says:

    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


  3. actually I need first column descending and second column ascending, but its only working if i use first column ascending and second column descending.

  4. Dudeson says:

    Hi, is there a way to make mysql order by the first column (higher priority) followed by the second column (lower priority)? This is not the same as column_1, column_2 where it means only if column_1 is the same, column_2 will be checked? Can Group By be used in this regard? Thanks.

  5. yes You are right.

    if some values are same in column_1 then second sort will be applied between those values.


  6. Web Dizajn says:

    $sort=’DESC’;//will be the defualt

  7. jsherk says:

    If you need to combine two columns first and then sort based on the combination, you can do this:

    I have two columns `lastname` and `organization`. If one is filled then the other one is always empty. A normal SELECT * FROM table ORDER BY organization,lastname would list all the organizations first and then the lastnames second, but I wanted to intermix them, so I did this:

    SELECT * FROM table ORDER BY CONCAT(organization,lastname)

    This will combine the two columns for the ORDER BY without actually creating a new column, so that lastname SMITH would appear in between organization RESQ and organization TIMET.

  8. prakash says:

    hanks a lot boss.

  9. prakash says:

    Thanks a lot boss.

  10. plr store says:

    Hello can’t we use ORDER BY DESC when using two column names?

    ORDER BY column1, column2 DESC

    it doesnt work.

  11. Mouli says:

    Thanks… Its Working…

  12. muhammed says:

    I want sort table by using one column,Some case the value might be same ,Such cases I want to sort or swap that field by using another column.Please help me

  13. Neil says:


  14. Sowmya says:

    when we click on the table fields order by action has to be performed for every field when ever clicked on the field like when we click on the name filed it has to perform the order by in asc and dsc
    so plz help me out in doing so

Leave a Reply