【发布时间】:2016-12-06 14:18:12
【问题描述】:
我无法理解查询,我找到了获取数据的解决方法,但我想知道发生了什么。
所以基本上我想获取不包含在另一个表中的所有 id。如果我分别计算它们,我会得到:
select count(distinct product_sid)
from gaps_inp.pim_product_channel pc
where pc.channel_code = 'Amazon'
计数
200658
然后计算另一张桌子上的项目:
select count(w.sid)
from gaps_fend.product_whitelist w
where w.channel_code = 'Amazon'
计数
39697
但现在如果我尝试计算差异:
select count(*)
from gaps_inp.pim_product_channel pc
where pc.channel_code = 'Amazon'
and pc.product_sid not in (
select w.sid
from gaps_fend.product_whitelist w
where w.channel_code = 'Amazon'
);
计数
0
gaps_inp.pim_product_channel.product_sid 和 gaps_fend.product_whitelist.sid 两个字段都是 bigint
我可以通过使用 left join 和 其中 sid 为 null 来做到这一点,但我仍然想知道我在 中做错了什么>where not in 查询。
这是解决方法:
select count(distinct pc.product_sid)
from gaps_inp.pim_product_channel pc
left join gaps_fend.product_whitelist w on w.channel_code = 'Amazon' and pc.product_sid = w.sid
where pc.channel_code = 'Amazon'
and w.sid is null;
计数
160968
【问题讨论】:
标签: sql postgresql postgresql-9.2