MySQL Select NULL

MySQL Select NULL


How to select rows from MySQL table where certain field is empty is a problem i stuggled against for a while like … 10 minutes or so, while solution is quite simple. Remember that it will work with MySQL this solution was not tested with other databases and as far as i am concerned it does not work with Sybase.


SELECT * FROM `table` WHERE `field1` IS NULL
There is also variations for this:


SELECT * FROM `table` WHERE ISNULL(`field1`)
While browsing MySQL documentation i found one more interesting function IFNULL(expr1, expr2). What it basically do is: if expr1 is NULL then it returns expr2 else it returns expr1. Here is a sample usage:


mysql> SELECT IFNULL(1,0);
-> 1
mysql> SELECT IFNULL(NULL,10);
-> 10
mysql> SELECT IFNULL(1/0,10);
-> 10
mysql> SELECT IFNULL(1/0,'yes');
-> 'yes'
That’s it for now, i hope you will find it helpful.

No comments:

Post a Comment

Keep ur coding aside.. Relax for some time..