Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Friday, July 27, 2012

Difference between find_in_set and in MySQL

If you are confused with these functions then there is short explanation

They are both used for matching a single value again comma separated multiple values

IN: Works when you are looking for database/column value against commas separated list
e.g. WHERE id IN (1,2,3)

FIND_IN_SET: Works the other way around i.e. when you are looking for a single values against a comma separated list stored in a column
e.g. WHERE FIND_IN_SET('name',namesColumn)
where names is a column having value like ('john,paul,richard')

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