我没有按预期得到输出,因为
AND ta.task_status_type_id NOT IN ( 10 )
不适用于以下查询.
SELECT ta.task_id AS id,u.employee_id AS employee_id,ta.task_status_type_id FROM task_assignments AS ta,users AS u WHERE u.id = ta.user_id AND ta.id IN ( SELECT max ( ta.id ) OVER ( partition BY ta.task_id ) AS id FROM task_details AS td,task_assignments AS ta WHERE td.task_id = ta.task_id AND td.developer_employee_id IS NULL AND ta.task_type_id IN(6,7) AND ta.task_status_type_id NOT IN ( 10 ) AND ta.task_status_type_id IN ( 9 ) );
一个有根据的猜测(缺乏更多信息):
原文链接:https://www.f2er.com/postgresql/192817.html如果涉及任何NULL值且测试值不在列表中,NOT IN(…)将返回NULL.但只有TRUE符合WHERE子句.
a NOT IN (b,c)
转变为:
a <> ALL ('{b,c}'::sometype[])
相当于:
(a <> b AND a <> c )
如果这些值中的任何一个(在运算符的任一侧)为NULL,则得到:
(NULL AND FALSE)
那是:
NULL
并且NULL在WHERE子句中等效于FALSE.只有TRUE才有资格.
众所周知,这会导致不熟悉tri-valued logic的用户不相信.
请改用IS DISTINCT FROM
或NOT EXISTS
.或LEFT JOIN / IS NULL
.
示例(更多猜测)
在这种特殊情况下,您根本不需要有罪的表达
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 )
如果您秘密使用要排除的值列表,可以与包含列表共享元素:
... AND (ta.task_status_type_id IN ( ... )) IS NOT TRUE ...
或者你清除了NULL值.或者您在包含和排除列表中避免使用常见元素.