Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does null seem to equal an integer in WHERE?

I am doing a query with a trivial join, with the intention of finding the newest row the user hasn't voted on yet:

SELECT
    v.user_id,
    v.version_id,
    vv.user_id
FROM versions v
LEFT JOIN versions_votes vv ON v.version_id = vv.version_id
WHERE vv.user_id != 39;

Curiously, this returns no rows if vv.user_id is null. My admittedly pedestrian understanding of the problem is that NULL cannot be equal to anything - that's why we have to test for IS NULL rather than = NULL in the first place.

And yet, here we are. If I modify the WHERE clause as follows:

WHERE (vv.user_id != 39 OR vv.user_id IS NULL)

the query appears to work properly (and also appears to confirm that NULL is evaluating to 39.

like image 316
Winfield Trail Avatar asked Aug 23 '26 16:08

Winfield Trail


1 Answers

You are right that "NULL cannot be equal to anything".
What you are missing is that NULL cannot be unequal, either.

NULL compared to anything is always NULL. The problem at hand is that you got the LEFT JOIN wrong. This should work:

SELECT v.user_id, v.version_id, vv.user_id
FROM   versions v
LEFT   JOIN versions_votes vv ON v.version_id = vv.version_id
                             AND vv.user_id = 39
WHERE  vv.version_id IS NULL
ORDER  BY v.created
LIMIT  1;

You had an additional condition referencing vv in the WHERE clause: AND vv.user_id != 39. Probably expecting that NULL != 39 would qualify, but it doesn't. See:

  • Query with LEFT JOIN not returning rows for count of 0

There are basically three different techniques to do this:

  • Select rows which are not present in other table
like image 101
Erwin Brandstetter Avatar answered Aug 25 '26 05:08

Erwin Brandstetter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!