Thursday, April 5, 2012

CASE WHEN option in MySQL

MySQL will return conditional based values without using if condition. Without using if conditions, we can use CASE WHEN option in MySQL.

For example,

For example: table name: students
snonamemark
1Sachin39
2Dravid90
3Kohli30
4Ganguly66
Output: Pass mark is above 40
snonameresult
1SachinFail
2SachinPass
3KohliFail
4GangulyPass
Query:
SELECT sno, name, CASE WHEN  mark >= 40 THEN 'Pass' ELSE 'Fail' END AS Result FROM student