NOT IN in postgresql not working [closed]

旧巷老猫 提交于 2019-11-28 13:14:49
Erwin Brandstetter

An educated guess (for lack of more information):

NOT IN (...) returns NULL if any NULL values are involved and the tested value is not in the list. But only TRUE qualifies in a WHERE clause.

a NOT IN (b,c)

is transformed to:

a <> ALL ('{b,c}'::sometype[])

equivalent to:

(a <> b AND a <> c )

If any of these values (on either side of the operator) is NULL, you get:

(NULL AND FALSE)

That's:

NULL

And NULL is equivalent to FALSE in a WHERE clause. Only TRUE qualifies.

This has been known to cause disbelieve in users unfamiliar with tri-valued logic.

Use IS DISTINCT FROM or NOT EXISTS instead. Or LEFT JOIN / IS NULL.

Example (more guesswork)

In this particular case, you don't need the incriminated expression at all

SELECT ta.task_id AS id
      ,u.employee_id
      ,ta.task_status_type_id
FROM   task_assignments ta
JOIN   users            u  ON u.id = ta.user_id
WHERE  ta.id IN (
   SELECT max(ta.id) AS id
   FROM   task_details     td
   JOIN   task_assignments ta USING (task_id)
   WHERE  td.developer_employee_id IS NULL
   AND    ta.task_type_id IN (6,7)
-- AND    ta.task_status_type_id IS DISTINCT FROM 10 -- just cruft
   AND    ta.task_status_type_id = 9                 -- this expression covers it
   GROUP  BY ta.task_id
   )

If you are secretly using a list of values to be excluded that could share elements with the inclusion list:

... 
    AND    (ta.task_status_type_id IN ( ... )) IS NOT TRUE
...

Or you weed out NULL values.
Or you avoid common elements in inclusion and exclusion list.

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