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.

2
  • 1
    A nullable int column can be either an int or NULL - it can't be an empty string. Commented Feb 5, 2013 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 :) Commented Feb 5, 2013 at 18:17

4 Answers 4

9

You can use IFNULL function in MySQL.

select ____
  from tbl
 where IFNULL(content_id, 0) = 0
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

2

You can try COALESCE:

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

1 Comment

Coalesce is mobile to all RDBMS platforms. Check here for a comparison of SQL ISNULL(), NVL(), IFNULL() and COALESCE() Functions
0

Could this work for you?

SELECT *
FROM table
WHERE id IN ('21')
AND content_id IN (NULL, 0, '');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.