Mysql Left Join Multiple Order Bys

Discussion in 'PHP' started by Dman91, Dec 22, 2008.

  1. #1
    SELECT *
    FROM table1 AS t1
    LEFT JOIN table2 AS t2 ON t2.id = t1.sid
    ORDER BY t1.id DESC , t2.rating DESC
    LIMIT 30 , 30 
    Code (markup):
    Alright this is my SQL code
    Description - id is the submission id. sid is site id. rating is the rating of the article. What I'm trying todo, get set of latest results, ordered by submission ID in descending order and those results, ordered by rating. ATM the SQL query only orders by submission id in descending order and seems to ignore the article rating. Any ideas?
     
    Dman91, Dec 22, 2008 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    When you have multiple 'order by' fields, the first one takes precedence. The second 'order by' field is used only when there are multiple rows with the same value for the first 'order by' field.

    In the case above, it looks like t1.id is a primary key, which won't have any duplicates and therefore the second 'order by' is redundant.
     
    phper, Dec 22, 2008 IP