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 > Case, Subquery, Greater than Comparison

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-20-09, 05:21
NickJ NickJ is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
Case, Subquery, Greater than Comparison

Hi,

Is it possible to have a subquery within a case statement and perform greater than / less than comparisons on the value returned in the subquery?

i.e.
Code:
select case (select price from product)
            when < 100 then 'cheap'
            when between 101 and 150 then 'fairly expensive'
            else 'expensive';
Sorry for the for example, but you get the idea?

Thanks,
nickj

Last edited by NickJ; 06-20-09 at 05:45.
Reply With Quote
  #2 (permalink)  
Old 06-20-09, 06:01
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Code:
SELECT case 
             when price < 100 then 'cheap'
             when price < 150 then 'fairly expensive'
             else 'expensive'
           END
FROM product;
Reply With Quote
  #3 (permalink)  
Old 06-20-09, 07:09
NickJ NickJ is offline
Registered User
 
Join Date: Sep 2003
Posts: 12
No, the input expression for the case statement needs to come from a subquery.

So the following would work...

Code:
SELECT case 
             when (select price from product) < 100 then 'cheap'
             when (select price from product) < 150 then 'fairly expensive'
             else 'expensive'
           END
... but wanted to do this withouth having repeat the subquery each time.
Reply With Quote
  #4 (permalink)  
Old 06-20-09, 08:10
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by NickJ
No, the input expression for the case statement needs to come from a subquery.
Why?
Can you show us the whole picture?
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