使用以下查询:
select id, c.actor_id, d.actor_id from community c
left join duplicate_community_rows_by_name_actor_id d on d.actor_id = c.actor_id
where c.actor_id like '%gameboy%';
我得到了结果(其中
<null>
是实际的空值):
151492,https://lemm.ee/c/gameboy,<null>
627389,https://lemmy.world/c/gameboy,https://lemmy.world/c/gameboy
55519,https://lemmy.world/c/gameboy,https://lemmy.world/c/gameboy
如果我修改查询以选择不具有空 actor_id 的结果:
select id, c.actor_id, d.actor_id from community c
left join duplicate_community_rows_by_name_actor_id d on d.actor_id = c.actor_id
where c.actor_id like '%gameboy%' and d.actor_id IS NOT NULL;
我的结果减少了 2,只剩下一个元素:
627389,https://lemmy.world/c/gameboy,https://lemmy.world/c/gameboy
我尝试过各种加入方式。左、右、外、内。它们都不起作用。我对加入这里有什么误解?为什么
where
子句会影响连接?
duplicate_community_rows_by_name_actor_id
表的创建如下:
create temporary table duplicate_community_rows_by_name_actor_id(name text, actor_id text);
select * from duplicate_community_rows_by_name_actor_id;
copy pg_temp.duplicate_community_rows_by_name_actor_id from '/var/lib/postgresql/data/duplicates.csv' delimiter ',' csv;
select * from duplicate_community_rows_by_name_actor_id where name like '%gameboy%';
这是 postgres 16。
如果没有一些示例数据,很难判断问题出在哪里,但某些内容一定与有关附加Where子句条件的问题中描述的不同:
...并且 d.actor_id 不为空
如果你的数据是这样的:
-- S a m p l e D a t a :
Create Temporary Table duplicate_community_rows_by_name_actor_id( name text, actor_id text );
Insert Into duplicate_community_rows_by_name_actor_id
Select 'NAME X', 'https://lemmy.world/c/gameboy'
--
Select *
From duplicate_community_rows_by_name_actor_id;
名字 | 演员_id |
---|---|
姓名 X | https://lemmy.world/c/gameboy |
-- S a m p l e D a t a :
Create Table comunity(id Int, actor_id text);
Insert Into comunity
Select 151492, 'https://lemm.ee/c/gameboy' Union All
Select 627389, 'https://lemmy.world/c/gameboy'Union All
Select 55519, 'https://lemmy.world/c/gameboy';
Select * From comunity;
id | 演员_id |
---|---|
151492 | https://lemm.ee/c/gameboy |
627389 | https://lemmy.world/c/gameboy |
55519 | https://lemmy.world/c/gameboy |
...那么您的第一个查询...
Select c.actor_id, d.actor_id
From comunity c
Left Join duplicate_community_rows_by_name_actor_id d ON(d.actor_id = c.actor_id)
Where c.actor_id like '%gameboy%'
Order By c.actor_id;
...结果为:
演员_id | 演员_id |
---|---|
https://lemm.ee/c/gameboy | 空 |
https://lemmy.world/c/gameboy | https://lemmy.world/c/gameboy |
https://lemmy.world/c/gameboy | https://lemmy.world/c/gameboy |
上面的 Null 是 Null - 不是一个值,只是 Null 表示缺少任何类型的值(无)
可以使用提到的附加Where条件从结果集中过滤掉具有Null的行。
这不应该、不能、不能影响其余的结果行(甚至不重复)。这只会排除第一行。
Select c.actor_id, d.actor_id
From comunity c
Left Join duplicate_community_rows_by_name_actor_id d ON(d.actor_id = c.actor_id)
Where c.actor_id like '%gameboy%' And d.actor_id Is Not Null
Order By c.actor_id;
演员_id | 演员_id |
---|---|
https://lemmy.world/c/gameboy | https://lemmy.world/c/gameboy |
https://lemmy.world/c/gameboy | https://lemmy.world/c/gameboy |
...将使用 Inner Join 获取相同的结果集,无需额外的 Where 条件(IS NOT NULL)。
Select c.actor_id, d.actor_id
From comunity c
Inner Join duplicate_community_rows_by_name_actor_id d ON(d.actor_id = c.actor_id)
Where c.actor_id like '%gameboy%';
演员_id | 演员_id |
---|---|
https://lemmy.world/c/gameboy | https://lemmy.world/c/gameboy |
https://lemmy.world/c/gameboy | https://lemmy.world/c/gameboy |
为什么 where 子句会影响连接?
where 子句不影响连接 - where 子句只是在选择连接数据后过滤结果集...
在您的实际环境中一定有其他因素导致了您所呈现的结果。