...

View Full Version : select query - order by help



twobyfour
06-24-2010, 03:37 AM
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


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

Keleth
06-24-2010, 03:57 AM
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.

twobyfour
06-24-2010, 04:22 AM
can i do multiple subselects to select the different entries in order to show them properly

Keleth
06-24-2010, 04:29 AM
You could do a statement that selects only free and gpl, sorted ascending, then union on the commercial?

twobyfour
06-24-2010, 04:31 AM
i tried this but it doesnt work. Can you give me an example?


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

Keleth
06-24-2010, 05:08 AM
SELECT * FROM table WHERE license IN ('free', 'gpl') ORDER BY license UNION SELECT * FROM table WHERE license = 'commercial'

twobyfour
06-24-2010, 03:25 PM
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 ()

Keleth
06-24-2010, 03:50 PM
Hm... strange, I've done it without parenthesis, but hey, if it works, great!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum