Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    146
    Thanks
    8
    Thanked 0 Times in 0 Posts

    select query - order by help

    I need to sort a select query.

    Table is as follows:


    license |
    free |
    commercial |
    free |
    gpl |
    free |
    gpl |
    commercial |

    I need this to order by all rows with free in the license column first and then gpl and then commercial.

    How can i do select statement and use order by license and the entry to sort it like this:

    free
    free
    free
    gpl
    gpl
    commercial
    commercial

    PHP Code:
    SELECT FROM ------- order by license and input";
    $result = @mysql_query($sql,$connection) or die(mysql_error()); 

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Without doing a subselect, I'm not sure you can do it this way. Best as I know, you can only sort by the default order, ascending, or descending.

  • #3
    Regular Coder
    Join Date
    May 2010
    Posts
    146
    Thanks
    8
    Thanked 0 Times in 0 Posts
    can i do multiple subselects to select the different entries in order to show them properly

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    You could do a statement that selects only free and gpl, sorted ascending, then union on the commercial?

  • #5
    Regular Coder
    Join Date
    May 2010
    Posts
    146
    Thanks
    8
    Thanked 0 Times in 0 Posts
    i tried this but it doesnt work. Can you give me an example?

    PHP Code:
    SELECT FROM ------ WHERE license '(SELECT * FROM ------ WHERE license='Commercial')'"; 

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Code:
    SELECT * FROM table WHERE license IN ('free', 'gpl') ORDER BY license UNION SELECT * FROM table WHERE license = 'commercial'

  • Users who have thanked Keleth for this post:

    twobyfour (06-24-2010)

  • #7
    Regular Coder
    Join Date
    May 2010
    Posts
    146
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks for the example but i get this error: Incorrect usage of UNION and ORDER BY

    I fixed, the select statements need to be wrapped with ()
    Last edited by twobyfour; 06-24-2010 at 03:28 PM.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Hm... strange, I've done it without parenthesis, but hey, if it works, great!


  •  

    Posting Permissions

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