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

11 Responses

  1. 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

    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

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

    Thanks

  6. if(!isset($_GET['sort'])==’ASC’){
    $sort=’DESC’;
    $sort_link=’ASC’;
    $sort_link2=’ASC’;
    }else{
    $sort=’DESC’;//will be the defualt
    $sort_link=’ASC’;
    $sort_link2=’ASC’;

  7. jsherk

    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

    hanks a lot boss.

  9. prakash

    Thanks a lot boss.

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

    ORDER BY column1, column2 DESC

    it doesnt work.

  11. Thanks… Its Working…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Next Post » »