有两个客户基本信息表:需要将该两个客户基本信息表信息同步。现状:两个客户基本信息表都有重复的数据,可以不去更新重复的数据。
     需求:通过p_customer_temp表更新custbaseinfo表数据,条件:通过 p_customer_temp表的cardid 匹配custbaseinfo表。
      我的SQL语句:
select count(1) from p_customer_temp pc1    where exists 
       (select 1 from ( select cardid from p_customer_temp pct where pct.cardid is not null  and pct.flag ='1' group by cardid having  count(cardid) =1 ) pc2 where pc1.cardid = pc2.cardid) 
        and not exists (select 1  from custbaseinfo cust   where exists(select 1 from custbaseinfo cust1 where cust1.crednum = cust.crednum) 
        and  pc1.cardid = cust.crednum  group by cust.crednum having count(cust.crednum)>1 )
      求精辟的SQL语句!!!求优化!!!

解决方案 »

  1.   

    优化不仅仅是SQL语句本身能解决的问题
    有时候要从数据库本身入手 索引 分区 临时表 少一些连接查询
    并不是一条SQL都得到结果的方法就是好方法
      

  2.   

    你这写的太复杂了。很简单的问题你写的那么复杂。直接就是INSERT INTO完了把不在custbaseinfo表中的p_customer_temp 他的数据插入就可以了。哪那么复杂。
      

  3.   

    你sql写太罗嗦,说明表结构,需求,给出数据,和想得到的结果。
      

  4.   

    能不能这样,在被插入表中设置客户名为主键,之后使用plsql语句插入:
    begin
     insert into t1
    select * from t2
    when exception then 
     null;
    end;