表1,表2列完全一样,比如都是A,B两个列。
取出表1,2中A列字段个数不一样,以及A列字段个数一样但B不一样的A列,SQL应该怎么写如:表1:
A  B 
1  100
1  101
2  103
3  104表2:
A  B
1  100
2  103
3  109表1中A=1的有两个,表2中是1个,取出A=1
表1中A=2的有1个,表2中也是1个,且B字段也相等,不取出
表1中A=3的有1个,表2中也是1个,但B字段不相等,取出A=3。这个SQL应该怎么写,谢谢了。

解决方案 »

  1.   

    with
    tab_a as (
      select A,count(*) A_num
      from tab_1
      group by A
    ),
    tab_b as (
      select A,count(*) A_num
      from tab_2
      group by A
    )
    select tab_A.A from tab_A,tab_B
    where tab_A.A=tab_B.A and tab_A.A_num<>tab_B.A_num
    union all
    select A
    from tab_1 ,tab_2
    where tab_1.A=tab_2.A
    and tab_1.B<>tab_2.B
    and tab_1.A in (
      select tab_a.A from tab_A,tab_B
      where tab_A.A=tab_B.A and tab_A.A_num=tab_B.A_num
    )
    and tab_2.A in (
      select tab_a.A from tab_A,tab_B
      where tab_A.A=tab_B.A and tab_A.A_num=tab_B.A_num
    )
      

  2.   

    with
    tab_a as (
      select A,count(*) A_num
      from tab_1
      group by A
    ),
    tab_b as (
      select A,count(*) A_num
      from tab_2
      group by A
    )是什么意思?