counter customizable free hit

Tuesday, July 26, 2005

IF in SQL statements

In older versions of Oracle you can use DECODE to implement conditional logic within SQL statements, from 8i you can also use CASE. In MySQL the decode functionality is replicated using IF.

IF is infact a function call rather than a part of the SQL syntax but it works pretty well in most cases. For example...

mysql> select if(emp_id=1,'X','Y') as test, emp_id from emps;
+------+--------+
| test | emp_id |
+------+--------+
| X | 1 |
| Y | 2 |
| Y | 3 |
+------+--------+
3 rows in set (0.06 sec)

0 Comments:

Post a Comment

<< Home