[英]Join or Sub-query to “detect missing records”?
I'm struggling to get this query working. 我正在努力使此查询正常工作。
I have 2 tables that contain a countrycode being 'US' or 'AU' or 'JP' - this is a common key. 我有2个表,其中包含一个国家代码为“ US”或“ AU”或“ JP”-这是一个公共键。
SELECT DISTINCT cc FROM geo_world; SELECT DISTINCT cc FROM geo_world; SELECT cc FROM geo_country; SELECT cc FROM geo_country;
Note: geo_world is a large table with millions of entries but only 124 distinct cc values. 注意:geo_world是一个大型表,具有数百万个条目,但只有124个不同的cc值。
Note: geo_country contains only 244 cc values. 注意:geo_country仅包含244 cc值。
I want to find out the cc values in geo_country that are not show in geo_world. 我想找出geo_world中未显示的cc值。 There should be 120 of them. 应该有120个。
thankyou - I've tried joins but failed to get the result. 谢谢-我尝试了加入,但没有得到结果。
In principle, any of these should work: 原则上,任何这些都应该起作用:
SELECT cc
FROM geo_country
WHERE cc NOT IN
( SELECT cc -- or SELECT DISTINCT cc
FROM geo_world
)
;
SELECT cc
FROM geo_country
WHERE NOT EXISTS
( SELECT 1
FROM geo_world
WHERE cc = geo_country.cc
)
;
SELECT geo_country.cc
FROM geo_country
LEFT
OUTER
JOIN geo_world
ON geo_world.cc = geo_country.cc
WHERE geo_world.cc IS NULL -- i.e., the join failed
;
But you'll have to try them to see which one performs best. 但是,您必须尝试让他们看看哪个效果最好。
SELECT DISTINCT cc
FROM geo_world
WHERE cc NOT IN (SELECT cc FROM geo_country)
Some notes: 一些注意事项:
geo_world
has an index that covers cc
确保geo_world
的索引包含cc
You can use NOT EXISTS condition. 您可以使用NOT EXISTS条件。 You can do this like this: 您可以这样做:
SELECT cc FROM geo_country WHERE
NOT EXISTS (SELECT * from geo_world WHERE geo_world.cc = geo_country.cc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.