数据库里有A,B,C三张表 ,每张表里都有20万条记录,每张表里都有一个客户ID,现在想检查出同时存在一张表以上的客户ID(也是说,同时存在两2张表或者2张表的客户id)。注意性能。
请问有什么好的方法。

解决方案 »

  1.   

    select A.客户id from A inner join B on A.客户id=B.客户id
    union
    select A.客户id from A inner join C on A.客户id=C.客户id
    union 
    select B.客户id from B inner join C on B.客户id=C.客户id--客户id加索引
      

  2.   

    select distinct 客户ID 
    from A 
    where exists(select 1 from B where 客户ID=A.客户ID)
    or exists(select 1 from C where 客户ID=A.客户ID)
    union 
    select distinct 客户ID
    from B 
    where exists(select 1 from C where 客户ID=B.客户ID)