Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL query for empty and NULL value together

Tags:

sql

mysql

In a database, among the fields, I have two fields which are picName and UserName. The picName can have a NULL value, a picture's name or it can be empty.

I want to query the userName of those entries whose picName is either NULL or empty.

I tried these commands separately

Select userName from user where picName = ''

Select userName from user where picName IS NULL

But I want to get both the results in 1 query.

I tried using IN operator like this

Select userName from user where picName IN ('' , IS NULL)

But it didn't work.

How should I do it...?

like image 835
U.B.A.R Avatar asked Oct 17 '12 17:10

U.B.A.R


2 Answers

use OR if you have multiple filters in your condition

Select userName 
from user 
where picName IS NULL OR 
      picName = ''
like image 85
John Woo Avatar answered Oct 19 '22 08:10

John Woo


An alternative to the other answers is to use MySQL's IFNULL() or COALESCE() functions:

SELECT userName FROM user WHERE IFNULL(picName='',TRUE);
SELECT userName FROM user WHERE COALESCE(picName,'') = '';
like image 45
eggyal Avatar answered Oct 19 '22 10:10

eggyal