Why does null equal integer in WHERE?

一笑奈何 提交于 2019-12-11 09:48:30

问题


I am doing a query with a trivial join, with the intention of finding the newest record that 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 - and 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.


回答1:


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. More details in this related answer:
Query with LEFT JOIN not returning rows for count of 0

There are basically three techniques to do this:
Select rows which are not present in other table



来源:https://stackoverflow.com/questions/23618167/why-does-null-equal-integer-in-where

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!