SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot iStudent's Avatar
    Join Date
    Mar 2004
    Location
    So Cal
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL Sort Command

    I am looking for an SQL command that will sort the ID column in my database in ascending order.


    Home Page
    Last edited by iStudent; Sep 11, 2004 at 09:32.

  2. #2
    SitePoint Addict matt_12511's Avatar
    Join Date
    Feb 2004
    Location
    Michigan
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    **EDIT **

    oops the correct response is on the post below.


    Matt

  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ORDER BY ID ASC

  4. #4
    SitePoint Zealot iStudent's Avatar
    Join Date
    Mar 2004
    Location
    So Cal
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried both and either of them worked. Do I have to put the table name in some where?

  5. #5
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, the ORDER BY clause comes after everything in the SQL statement.

    Code:
    SELECT
      ID
    FROM
      table_name
    WHERE
      <conditions>
    ORDER BY
      ID ASC

  6. #6
    SitePoint Addict matt_12511's Avatar
    Join Date
    Feb 2004
    Location
    Michigan
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The entire SQL command is"

    Select Field Names From Table Name ORDER BY [/I]ID Field Name[I] ASC.

    or
    Select * From Customers Order By CustomerID ASC


    or Select CustomerID, CustFirstName, CustLastName From Customers Order By CustomerID ASC


    try those,

    Matt

    PS. Do you have to have the ASC? or is it default?

  7. #7
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes ASC (ascending) is the default, but I put it there so that there's no doubt about it next time when I read the SQL (I always can't remember which is the default).

  8. #8
    SitePoint Zealot iStudent's Avatar
    Join Date
    Mar 2004
    Location
    So Cal
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do I put for the conditions? I just want it to sort the whole table.

  9. #9
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have no conditions, just leave it out.

    Code:
    SELECT
      ID
    FROM
      table_name
    ORDER BY
      ID ASC

  10. #10
    SitePoint Zealot iStudent's Avatar
    Join Date
    Mar 2004
    Location
    So Cal
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that. I changed the code to:

    Code:
    SELECT
      *
    FROM
      home
    ORDER BY
      ID ASC
    It does sort things, but only on the SQL query/queries page in phpmyadmin. When I got to the browse tab in phpmyadmin, nothing is changed.

  11. #11
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you are mistaken in your understanding of databases. The records in a table are not sorted in any determinable way. So you can't say that this record is record 1, that one is record 2 in the table.

    When you view the records in, say, phpmyadmin, there is no specific ordering given to the records. If you want to view the records in a specific order, use the ORDER BY clause in an SQL statement. However, you can't change the innate order because there is none.

  12. #12
    SitePoint Zealot iStudent's Avatar
    Join Date
    Mar 2004
    Location
    So Cal
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I get it now. Thanks for all your help!


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
  •