MYSQL Shorthand If/Else Statement

I seem to forget this frequently and it is quite useful, so what better place to put it than here?

The IF() function in mysql is similiar to the PHP shorthand if statement:

PHP: ($var) ? ‘Var is true’ : ‘Var is false’;
MySQL: IF(COLUMN > 1,true,false)

SELECT count(U.id) as Active,
SUM(IF(US2.id IS NOT NULL,1,0) ) as Canceled
FROM User
WHERE DateAdded BETWEEN '2009-09-01' AND '2009-09-02' 

MySQL IF() Function Documentation

Wednesday, December 2nd, 2009 MySQL

1 Comment to MYSQL Shorthand If/Else Statement

  • Warren says:

    Finally… i never really knew what the word was for these ‘quick’ conditions… like in php echo 1==2 ? ‘No’ : ‘Yes’;

    Thanks

  • Leave a Reply

    You must be logged in to post a comment.

    [ad code=1 align=center]