Multiple IF statements on MYSQL

John Nuñez picture John Nuñez · Mar 10, 2012 · Viewed 52.6k times · Source

I'm trying to Display somes values in my database result, I am using this code but I can not succeed:

SELECT item_code, IF(category_code = 'HERR1', 'NO', 1) OR (category_code = 'COLN5', 'NO', 2) AS category_code, item_name, item_quantity FROM qa_items

EDIT : I Want to display for example:

If category_code = 'HERR1'
 Display = 1
else if category_code = 'COLN5'
 Display = 2
End If

If anyone has any idea, would greatly appreciate it

Answer

a1ex07 picture a1ex07 · Mar 10, 2012

I'd rather use CASE :

SELECT item_code, 
CASE category_code 
WHEN 'HERR1' THEN 1
WHEN 'COLN5' THEN 2
ELSE 'NO'
END as category_code, item_name, item_quantity 
FROM qa_items

But IF will also work : IF(category_code='HERR1',1, IF(category_code='COLN5',2,'NO'))