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 > ORDERing by WHERE condition

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-14-07, 11:44
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
ORDERing by WHERE condition

Hi

Code:
SELECT * FROM `tbl` WHERE `Name` = 'John' OR `Name` LIKE '%John%'
Lets say the result has 10 rows, 4 from `Name` = 'John' and 6 from `Name` LIKE '%John%'.

I want the order such that the first 4 rows are from `Name` = 'John' and the last 6 rows are from `Name` LIKE '%John%'.

Is that possible ?

Thanks
__________________
MySQL 5.1
Reply With Quote
  #2 (permalink)  
Old 08-14-07, 11:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT some
     , specific
     , columns
  FROM `tbl` 
 WHERE Name LIKE '%John%'
ORDER
    BY CASE WHEN Name='John'
            THEN 42
            ELSE 937 END
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-14-07, 12:19
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
1. Couldnt get the THEN 42 ELSE 937 part. Are you referring to a STORED PROCEDURE ?

2. Is it possible if the query was
Code:
SELECT * FROM `tbl` WHERE `Name` = 'John' OR `Name` LIKE '%Smith%'
__________________
MySQL 5.1
Reply With Quote
  #4 (permalink)  
Old 08-14-07, 12:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
1. no, i'm not

2. yes, it is

please, why don't you do me a very small favour, and actually try my query
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-21-07, 10:44
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
This is excellent r937 - I did this and worked like a charm !
Code:
SELECT * FROM `tbl` WHERE
`Name` LIKE '%an%'
ORDER BY
CASE
WHEN `Name` LIKE 'an%' THEN 1
WHEN `Name` LIKE '%an' THEN 1000
ELSE 500
END
I think I got how this is sorting the result-set based on the CASE value matching against the WHERE clause, but is there some detailed explanation to this ?

Docs doesnt have a sorting-by-where-clause example.

Thanks
__________________
MySQL 5.1
Reply With Quote
  #6 (permalink)  
Old 08-21-07, 11:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
it looks like you understand how CASE works

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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