9
SELECT * 
  FROM table WHERE id IN ('21') 
   AND (content_id IS NULL OR content_id = 0 OR content_id = '')

Is there a shorter way of writing this condition.

I have a int() column that could be either: NULL, 0 or EMPTY.

bonCodigo
  • 14,268
  • 1
  • 48
  • 91
John Magnolia
  • 16,769
  • 36
  • 159
  • 270
  • 1
    A nullable int column can be either an int or NULL - it can't be an empty string. – Sam Dufel Feb 05 '13 at 17:24
  • is content_id a int column or a varchar column? if it's an int column there's no need to check if content_id='' so I think I'll delete my answer :) – fthiella Feb 05 '13 at 18:17

3 Answers3

9

You can use IFNULL function in MySQL.

select ____
  from tbl
 where IFNULL(content_id, 0) = 0
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
4

I think the shorter way is this:

SELECT * 
FROM table
WHERE id IN ('21')
      AND COALESCE(content_id IN ('0', ''), 1)

content_id IN ('0', '') may assume these values:

  • True if content_id is either '0' or ''
  • Null if content_id IS Null
  • False otherwise.

If it's Null, COALESCE will return 1 here, which is equivalent to True.

Marnix.hoh
  • 1,556
  • 1
  • 15
  • 26
fthiella
  • 48,073
  • 15
  • 90
  • 106
2

You can try COALESCE:

SELECT * FROM table WHERE id IN ('21') 
AND COALESCE(content_id,0) =0;
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • Coalesce is mobile to all RDBMS platforms. Check here for a comparison of [SQL ISNULL(), NVL(), IFNULL() and COALESCE() Functions](http://www.w3schools.com/sql/sql_isnull.asp) – bonCodigo Feb 05 '13 at 17:32