user1066183 user1066183 - 1 year ago 106
SQL Question

Mysql 5.5 IF statement

Using Mysql 5.5. Trying to use IF statement:

mysql> SELECT description IF(1=1,'ok','no') FROM my_table;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF(1=1,'ok','no') FROM my_table at line 1


Answer Source

Correct syntax of IF() in mysql query is:

SELECT IF(condition, 'Some Result If True', 'Some Result If False'), OTHER_COLUMNS
FROM ...

so you should use

SELECT description,IF(1=1,'ok','no') FROM my_table;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download