0

I am trying to do a query that sees if fields are equivalent. However, whenever the field is NULL it returns a false result. I even tried doing the same thing with the column itself:

SELECT * FROM `mturk_completion` WHERE (`mturk_completion`.`imdb_url` =  
`mturk_completion`.`imdb_url` AND `mturk_completion`.`worker_id` = 'A3NF84Q37D7F35' )

And it only returns results where the column is not NULL. Why is this so, and how do I get around it?

2
  • 3
    Which field, specifically? Also, what is intended by this statement "mturk_completion.imdb_url = mturk_completion.imdb_url"? As far as I know, the contents of a column will always equal the contents of the same column. Commented Feb 17, 2015 at 3:33
  • 1
    Except if the column contains NULL values. The NULL values don't equate to other NULL values. The = operator just returns a NULL instead. Commented Feb 17, 2015 at 3:43

3 Answers 3

4

Your title is absolutely correct for any SQL implementation (not just MySQL). NULL is not equal to anything (including another NULL).

You need to use explicit IS NULL check or COALESCE() function (or its RDBMS-dependent alternatives) to set some default value in case of NULL.

Sign up to request clarification or add additional context in comments.

1 Comment

Also null-safe operator.
4

Your comparison of mturk_completion.imdb_url to itself is redundant and should always return True, except when mturk_completion.imdb_urlis Null, in which case it will return Null.

That's because the operator = returns either True, False when comparisons can be made or Null, when either of the two operators is Null

Try this to illustrate the situation.

SELECT 1 = NULL; -- returns NULL
SELECT 1 != NULL; -- also return NULL
SELECT ISNULL(1 = NULL); -- returns 1
SELECT ISNULL(1 != NULL); -- returns 1

If you rewrite your query like below, your problems with ignoring NULLs will go away:

SELECT * FROM `mturk_completion` WHERE worker_id = 'A3NF84Q37D7F35'

Comments

1

I think you can use

(table.Field =  table2.Field OR COALESCE(table.Field, table2.Field) IS NULL)

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.