SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ORDER BY firstname lastname

    Hello,

    I have a MySQL table and I have 2 fields in a table:

    firstname & lastname

    How can I order the results alphabetically?

    ... ORDER BY firstname, lastname

    is NOT working.

    Please help.
    Why It Doesn't Work?!

  2. #2
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What error, if any are you getting from your query? Does the whole query fail or does it return not in order?
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!

  3. #3
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't have it order but two separate fields. You can ORDER BY lastname

    or

    ORDER BY firstname

    but not both.
    I PHP!

  4. #4
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From W3

    It is possible to order by more than one column. When ordering by more than one column, the second column is only used if the values in the first column are identical:

    Code:
    SELECT column_name(s)
    FROM table_name
    ORDER BY column_name1, column_name2
    You can order by two fields.

    Are you getting an error of some sort?
    No, I REALLY dislike having to use Joomla.

  5. #5
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I didn't know that. Now I know.
    I PHP!

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    angry, could you show the actual query? and a few rows of sample output?

    things to look for: leading blanks, upper/lower case, etc.
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you would want to

    ORDER by lastname, firstname

    otherwise it will order by firstname first.

  8. #8
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also if you want the orders to be descending instead of ascending you need to specify this

    ie ORDER BY lastname DESC, firstname DESC


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •