我在pgsql 9.5.1中有一些有趣的错误
我有2个表,其中包含相关数据contacts (id, name)
和jobs (id, contact_id, name)
。
我不确定此查询的有效性(考虑到后面介绍的奇怪行为)。>>
-- get unassigned contacts select * from contacts where id not in (select contact_id from jobs);
[在没有工作的情况下使用联系人id = 20的情况进行测试时,我得到了一些(IMO)奇怪的结果(在选择查询和联接等效项之间,结果存在显着差异)。
首先,我需要断言一些先决条件(步骤A)。接下来,我显示带有join的结果(步骤B)。最后,我使用子查询显示结果(步骤D)。(步骤C是D的补充请求,仅在此处突出显示我发现的奇怪内容。)
select count(distinct id) from contacts;
--> returns 10100
select count(distinct id) from jobs;
--> returns 12000
select count(distinct id) from contacts where id in (select contact_id from jobs);
--> returns 10000
select name from contacts where id=20;
--> returns "NAME"
select id from jobs where contact_id=20;
--> returns nothing (0 row)
select c.id, c.name, j.id
from contacts c
left join jobs j
on j.contact_id=c.id
where c.id=20;
--> returns 20, "NAME", <NULL>
select name from contacts where id in (select contact_id from jobs) and id=20;
--> returns nothing (0 row); (that's the expected result)
select name from contacts where id not in (select contact_id from jobs) and id=20;
--> returns nothing (0 row); (that's not the expected result - "NAME")
C和D查询得到相同的结果。
从逻辑上讲,这可能意味着在pgsql中:
id NOT IN (..values..) == id IN (..values..) FALSE == TRUE
“ pgsql专家”可以给我一个很好的解释,还是应该给联邦调查局打电话?
我在pgsql 9.5.1中有一个有趣的错误,我得到2个表,其中包含相关数据联系人(id,姓名)和工作(id,contact_id,姓名)。我不确定此查询的有效性(鉴于好奇...
您看到的是null
安全问题。如果not in
子查询返回的任何值为null
,则将忽略所有其他值。我们说not in
不是null安全的。