SQL using Order By with null

Discussion in 'Programming' started by weddingm, Jul 8, 2005.

  1. #1
    Hello,

    I am writing an sql code from a database using the ORDER BY command. It is sorting/listing "Business_Name" in that field. However, I have null or blank inputs in the "Business_Name" field.

    Is there a way to put the null or blank inputs at the bottom of the list instead of the top?

    $sql="SELECT * FROM listings ORDER BY Bus_State, Bus_City, Business_Name";

    Regards,
    Matt
     
    weddingm, Jul 8, 2005 IP
  2. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you not just avoid selecting NULL entries?

    SELECT * FROM listings WHERE Business_Name is not NULL ORDER BY Bus_State, Bus_City, Business_Name

    J.D.
     
    J.D., Jul 8, 2005 IP
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    And you can also order by ascending or descending
    SELECT * FROM listings WHERE Business_Name is not NULL ORDER BY Bus_State, Bus_City, Business_Name DESC
    SELECT * FROM listings WHERE Business_Name is not NULL ORDER BY Bus_State, Bus_City, Business_Name ASC
    Code (markup):
     
    exam, Jul 9, 2005 IP