Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-24-2010, 02:37 AM   PM User | #1
twobyfour
Regular Coder

 
Join Date: May 2010
Posts: 140
Thanks: 7
Thanked 0 Times in 0 Posts
twobyfour has a little shameless behaviour in the past
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()); 
twobyfour is offline   Reply With Quote
Old 06-24-2010, 02:57 AM   PM User | #2
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 06-24-2010, 03:22 AM   PM User | #3
twobyfour
Regular Coder

 
Join Date: May 2010
Posts: 140
Thanks: 7
Thanked 0 Times in 0 Posts
twobyfour has a little shameless behaviour in the past
can i do multiple subselects to select the different entries in order to show them properly
twobyfour is offline   Reply With Quote
Old 06-24-2010, 03:29 AM   PM User | #4
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
You could do a statement that selects only free and gpl, sorted ascending, then union on the commercial?
Keleth is offline   Reply With Quote
Old 06-24-2010, 03:31 AM   PM User | #5
twobyfour
Regular Coder

 
Join Date: May 2010
Posts: 140
Thanks: 7
Thanked 0 Times in 0 Posts
twobyfour has a little shameless behaviour in the past
i tried this but it doesnt work. Can you give me an example?

PHP Code:
SELECT FROM ------ WHERE license '(SELECT * FROM ------ WHERE license='Commercial')'"; 
twobyfour is offline   Reply With Quote
Old 06-24-2010, 04:08 AM   PM User | #6
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
Code:
SELECT * FROM table WHERE license IN ('free', 'gpl') ORDER BY license UNION SELECT * FROM table WHERE license = 'commercial'
Keleth is offline   Reply With Quote
Users who have thanked Keleth for this post:
twobyfour (06-24-2010)
Old 06-24-2010, 02:25 PM   PM User | #7
twobyfour
Regular Coder

 
Join Date: May 2010
Posts: 140
Thanks: 7
Thanked 0 Times in 0 Posts
twobyfour has a little shameless behaviour in the past
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 02:28 PM..
twobyfour is offline   Reply With Quote
Old 06-24-2010, 02:50 PM   PM User | #8
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
Hm... strange, I've done it without parenthesis, but hey, if it works, great!
Keleth is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:34 AM.


Advertisement
Log in to turn off these ads.