两个表有部分数据完全相同、有部分不相同如下,我想把A表中的B表中不同的数据查询出来,A表
字段A1
a
b
c
d
e
f
gB表
字段B1
a
c
f
g即查询输出
b
d
e请问这个SQL语句如何写?
十分感谢!

解决方案 »

  1.   

    select * from a where not exists(select 1 from b where a.a1=b.b1)
    union 
    select * from b where not exists(select 1 from a where a.a1=b.b1)
      

  2.   

    ---------------------------------------------------
    create table a(
    id char(2)
    )
    create table b(
    id char(2)
    )
    insert into a values('a')
    insert into a values('b')
    insert into a values('c')
    insert into a values('d')
    insert into a values('e')
    insert into a values('f')
    -------------------------
    insert into b values('a')
    insert into b values('c')
    insert into b values('e')
    insert into b values('s')
    insert into b values('m')
    select  c.id from a cross apply(select *from b where a.id=b.id)c
    /*
    id



    */
      

  3.   

    select a1 from a where a1 not in (select b1 from b)
    union all
    select b1 from b where b1 not in (select a1 from a)
    select a1 from a where not exists (select 1 from b where b.b1 = a.a1)
    union all
    select b1 from b where not exists (select 1 from a where a.a1 = b.b1)
      

  4.   

    create table A(a1 varchar(10))
    insert into a values('a')
    insert into a values('b')
    insert into a values('c')
    insert into a values('d')
    insert into a values('e')
    insert into a values('f')
    insert into a values('g')
    create table b(b1 varchar(10))
    insert into b values('a')
    insert into b values('c')
    insert into b values('f')
    insert into b values('g')
    go--1.如果仅仅是查A表不在B表的数据,则如下:
    select a1 from a where a1 not in (select b1 from b)
    /*
    a1         
    ---------- 
    b
    d
    e(所影响的行数为 3 行)
    */select a1 from a where not exists (select 1 from b where b.b1 = a.a1)
    /*
    a1         
    ---------- 
    b
    d
    e(所影响的行数为 3 行)
    */--2.如果是查两表相互不存在则如下:
    select a1 from a where a1 not in (select b1 from b)
    union all
    select b1 from b where b1 not in (select a1 from a)
    /*
    a1         
    ---------- 
    b
    d
    e(所影响的行数为 3 行)
    */
    select a1 from a where not exists (select 1 from b where b.b1 = a.a1)
    union all
    select b1 from b where not exists (select 1 from a where a.a1 = b.b1)
    /*
    a1         
    ---------- 
    b
    d
    e(所影响的行数为 3 行)
    */select a1 , 'A' [table] from a where a1 not in (select b1 from b)
    union all
    select b1 , 'B' [table]  from b where b1 not in (select a1 from a)
    /*
    a1         table 
    ---------- ----- 
    b          A
    d          A
    e          A(所影响的行数为 3 行)
    */select a1 , 'A' [table]  from a where not exists (select 1 from b where b.b1 = a.a1)
    union all
    select b1 , 'B' [table]  from b where not exists (select 1 from a where a.a1 = b.b1)
    /*
    a1         table 
    ---------- ----- 
    b          A
    d          A
    e          A(所影响的行数为 3 行)
    */drop table a , b
      

  5.   

    多谢,但不能运行,提示:
    “使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。 ”
      

  6.   

    改为:
    select a1 from a where not exists(select 1 from b where a.a1=b.b1)
    union
    select b1  from b where not exists(select 1 from a where a.a1=b.b1)
      

  7.   

    另一种方法:
    SELECT A1 FROM 
    (
    SELECT * FROM A 
    UNION ALL 
    SELECT *  FROM B
    ) a   GROUP BY a1 HAVING COUNT(a1)=1