If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > sorting on multiple criteria

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-29-08, 04:27
totus totus is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
sorting on multiple criteria

I'm trying to sort on multiple criteria. e.g. varchar, varchar, int, bigint. However, I want bigint to be sort from smallest to largest (if case bigint = 0) list those entries below the rest.

illustration:

sitename, siteurl, 4, 2324
sitename, siteurl, 5, 26248
sitename, siteurl, 4, 732456
sitename, siteurl, 6, 932400
sitename, siteurl, 2, 9932412
sitename, siteurl, 5, 23240876
sitename, siteurl, 2, 0
sitename, siteurl, 0, 0
sitename, siteurl, 1, 0

Is this possible?
Reply With Quote
  #2 (permalink)  
Old 10-29-08, 07:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
ORDER 
    BY sitename
     , siteurl
     , someint
     , CASE WHEN somebigint = 0 then 937 ELSE 0 END
     , somebigint
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-29-08, 08:31
totus totus is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Quote:
Originally Posted by r937
Code:
ORDER 
    BY sitename
     , siteurl
     , someint
     , CASE WHEN somebigint = 0 then 937 ELSE 0 END
     , somebigint
What does the 937 do? Doh! or is that just your handle?
Reply With Quote
  #4 (permalink)  
Old 10-29-08, 09:19
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
It's an abitrary number greater than the previous value used to determine the custom order.
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 10-29-08, 09:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
it is not "abitrary"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 10-29-08, 10:09
totus totus is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
I can't seem to get this to work, maybe its a php issue

$query = "SELECT `SiteName`, `SiteURL`, `PageRank`, `AlexaRank` FROM `SocialSites` WHERE `SiteName` LIKE '%$trimmed%' ORDER BY `AlexaRank`";

$numresults = mysql_query($query);
$numrows=mysql_num_rows($numresults);

...works fine.

no matter how I place: CASE WHEN `AlexaRank` = 0 then 937 ELSE 0 END within the string right before or after the ORDER BY statement I get an error.

Reply With Quote
  #7 (permalink)  
Old 10-29-08, 10:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
first of all, please dispense with the unnecessary and annoying backticks
Code:
SELECT SiteName
     , SiteURL
     , PageRank
     , AlexaRank 
  FROM SocialSites 
 WHERE SiteName LIKE '%$trimmed%' 
ORDER 
    BY CASE WHEN AlexaRank = 0 THEN 937 ELSE 0 END 
     , AlexaRank
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 10-29-08, 10:47
totus totus is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Quote:
Originally Posted by r937
first of all, please dispense with the unnecessary and annoying backticks
Code:
SELECT SiteName
     , SiteURL
     , PageRank
     , AlexaRank 
  FROM SocialSites 
 WHERE SiteName LIKE '%$trimmed%' 
ORDER 
    BY CASE WHEN AlexaRank = 0 THEN 937 ELSE 0 END 
     , AlexaRank
Still doesn't seem to work. I'm getting a Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource error.

Thanks helping out though. I truly appreciate it and am loving db design!

Reply With Quote
  #9 (permalink)  
Old 10-29-08, 11:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
that, sir, is not a mysql error, it is a generic php error

run your query outside of php to see whether it works or not, or else turn on proper php error detection
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 10-29-08, 11:25
totus totus is offline
Registered User
 
Join Date: Oct 2008
Posts: 14
Thanks 937, I got it firing now ...you Rock! I think I'm going to bail for a while, I've been up all night schooling myself. I can't tell you how much I appreciate your help.

Are you the dbforums master?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On