简体   繁体   English

加入或子查询以“检测丢失的记录”?

[英]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: 一些注意事项:

  1. Make sure that geo_world has an index that covers cc 确保geo_world的索引包含cc
  2. If you need to do that on regular basis - you probably better denormalize the data and keep unused countrycodes in another small 100-or-some rows table, just as some sort of cache 如果您需要定期执行此操作-最好对数据进行非规范化处理,并将未使用的国家/地区代码保留在另一个小的100左右行表中,就像某种缓存一样

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM