Tuesday, May 1, 2012

If else statement in Select MySQL

Ever wanted to show one thing for one value and another for rest as stored in the database? As an easy example if you have a column 'gender' that has 1 for male and 0 for female then instead of having to parse it before displaying you can simply do so using MySQL if statement. Here is the syntax

IF(condition, if true, if false)

SELECT name,IF(gender='1','Male','Female') AS gender
FROM person


No comments:

Post a Comment