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 > MySQL 'CASE'

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-04-04, 21:42
dhoward dhoward is offline
Registered User
 
Join Date: Jul 2004
Posts: 6
MySQL 'CASE'

I have a query in MSSQL like this:
Code:
select table1.*, 
CASE WHEN field1 > '10000' THEN '1'
WHEN field2 BETWEEN '5001' AND '9999' THEN '2'
WHEN field3 BETWEEN '2501' AND '5000' THEN '3'
WHEN field4 BETWEEN '0' AND '2500' THEN '4'
AS Class
FROM table1
How do I convert this to MySQL?

Last edited by dhoward; 08-04-04 at 21:45.
Reply With Quote
  #2 (permalink)  
Old 08-06-04, 12:59
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
I'd use MySQL's CASE, which should work just like MS-SQL's CASE. I'm highly suspicious that you didn't mean to use different column names (Field1 through Field4 in your example). That would be a bad thing in most cases!

-PatP
Reply With Quote
  #3 (permalink)  
Old 08-06-04, 14:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by dhoward
How do I convert this to MySQL?
by adding the manadory END keyword, which i believe is also mandatory in sql server

i agree with pat, youprobably did mean to use the same field name in each WHEN condition, right?
__________________
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