假设有两张表A和B,A表有两个字段:Key_A和Value_A;B表有两个字段:Key_B和Value_B。各用一个SQL语句实现如下功能:
1从A和B中查询出A.Key_A=B.Key_B的所有记录;
2将B表中的记录全部插入A表;insert into A select * from B
3查询出A表中存在而B表中不存在的记录;
4查询出A表和B表中完全相同的记录(A.Key_A=B.Key_B and A.Value_A=B.Value_B);
5当A.Key_A=B.Key_B时,将A表的Value_A字段更新为的B表Value_B字段;

解决方案 »

  1.   

    1
    select * from a,b where a.key_a = b.key_b;
    2
    insert into a select * from b;
    3
    select * from a where not exists(select 1 from b where b.key_b = a.key_a and b.value_b = a.value_a);
    4
    select * from a,b where a.key_a=b.key_b and a.value_a=b.value_b;
    5
    update a set value_a = (select value_b from b where b.key_b = a.key_a) where exists(select 1 from b where b.key_b = a.key_a);
      

  2.   

    1
    select * from a,b where a.key_a = b.key_b;
    2
    insert into a select * from b;
    3
    select * from a where not exists(select 1 from b where b.key_b = a.key_a and b.value_b = a.value_a);
    4
    select * from a,b where a.key_a=b.key_b and a.value_a=b.value_b;
    5
    update a set value_a = (select value_b from b where b.key_b = a.key_a) where exists(select 1 from b where b.key_b = a.key_a);