Sunday, 3 May 2015

Case Function in mysql

Case Function

SELECT CASE 10*2

WHEN 20 THEN ‘20 correct’

WHEN 30 THEN ‘30 correct’

WHEN 40 THEN ‘40 correct’

END;



--------------------------------------------------------------------------------------


SELECT DVDName, RatingID AS Rating,

CASE

WHEN RatingID=’R’ THEN ‘Under 17 requires an adult.’

WHEN RatingID=’X’ THEN ‘No one 17 and under.’

WHEN RatingID=’NR’ THEN ‘Use discretion when renting.’

ELSE ‘OK to rent to minors.’

END AS Policy

FROM DVDs

ORDER BY DVDName;




Or



SELECT DVDName, RatingID AS Rating,

CASE RatingID

WHEN ‘R’ THEN ‘Under 17 requires an adult.’

WHEN ‘X’ THEN ‘No one 17 and under.’

WHEN ‘NR’ THEN ‘Use discretion when renting.’

ELSE ‘OK to rent to minors.’

END AS Policy

FROM DVDs

ORDER BY DVDName;

No comments:

Post a Comment