Thursday, May 22, 2008

MySql IFNULL, IF, CASE-WHEN-THEN -ELSE-END

IFNULL(expr,expr1)
--------------------------------------------
If 1st_ expr is null then retrun 2nd_expr

SELECT IFNULL(1,0);
-> 1
SELECT IFNULL(NULL,10);
-> 10
SELECT IFNULL(1/0,10);
-> 10
SELECT IFNULL(1/0,'yes');
-> 'yes'
--------------------------------------------------------------
CASE WHEN condiion THEN expr1 ELSE expr3 END
--------------------------------------------------------------

SELECT (CASE 2 WHEN 1 THEN "one" WHEN 2 THEN "two" ELSE "more" END) as ans;
-> two

SELECT CASE WHEN 1<0 style="color: rgb(255, 0, 0);">THEN "true" ELSE "false" END;
->false
--------------------------------------------------------------
IF(condition,expr,expr1)
--------------------------------------------------------------

SELECT IF(0.1<>0,1,0);
-> 1
SELECT IF(STRCMP('test','test1'),'no','yes');
-> no

No comments: