select a.身份证,a.姓名 from 
[表1] a 
left join [表2] b on a.身份证=b.身份证
left join [表3] c on a.身份证=c.身份证
where b.身份证 is null and c.身份证 is null
union all
select a.身份证,a.姓名 from 
[表2] a 
left join [表1] b on a.身份证=b.身份证
left join [表3] c on a.身份证=c.身份证
where b.身份证 is null and c.身份证 is null
union all
select a.身份证,a.姓名 from 
[表3] a 
left join [表1] b on a.身份证=b.身份证
left join [表2] c on a.身份证=c.身份证
where b.身份证 is null and c.身份证 is null

解决方案 »

  1.   

    这样应该可以
    SELECT
    ISNULL(A.[姓名],ISNULL(B.[姓名],C.[姓名]))[姓名]
    FROM
    [表1] A
    FULL JOIN [表2] B ON A.[身份证]=B.[身份证]
    FULL JOIN [表3] C ON A.[身份证]=C.[身份证]
    WHERE
    A.[身份证]IS NULL OR B.[身份证]IS NULL OR C.[身份证]IS NULL
    GROUP BY ISNULL(A.[姓名],ISNULL(B.[姓名],C.[姓名]))
      

  2.   


    create table Users1
    (
      id int primary key identity(1,1),
      姓名 nvarchar(50),
      部门 nvarchar(50),
      生日 datetime,
      学历 nvarchar(50)
    )
    go
    create table Users2
    (
      id int primary key identity(1,1),
      姓名 nvarchar(50),
      部门 nvarchar(50),
      生日 datetime,
      学历 nvarchar(50)
    )
    gocreate table Users3
    (
      id int primary key identity(1,1),
      姓名 nvarchar(50),
      部门 nvarchar(50),
      生日 datetime,
      学历 nvarchar(50)
    )
    goinsert into Users1 values('张三','人事部','1989-12-12','中专')
    insert into Users1 values('刘邦','人事部','1976-8-23','本科')
    insert into Users1 values('项羽','人事部','1978-8-12','中专')
    insert into Users1 values('周杰伦','人事部','1989-12-12','中专')
    insert into Users1 values('张学友','技术部','1989-12-12','本科') --结果
    insert into Users1 values('张二','技术部','1989-12-12','本科')insert into Users2 values('张三','人事部','1989-12-12','中专')
    insert into Users2 values('刘邦','人事部','1976-8-23','本科')
    insert into Users2 values('项羽','人事部','1978-8-12','中专')
    insert into Users2 values('周杰伦','人事部','1989-12-12','中专')
    insert into Users2 values('张学友','技术部','1989-12-12','本科')
    insert into Users2 values('黄明鑫','技术部','1989-12-12','本科') --结果
    insert into Users2 values('黄明一','技术部','1989-12-12','本科')--结果--检索语句
    select a1.姓名 from users1 a1 where a1.姓名 not in (
    select a1.姓名 from users1 a1, users2 a2,users3 a3
    where a1.姓名=a2.姓名 and a1.姓名=a3.姓名 and a2.姓名=a3.姓名
    )
    union
    select a2.姓名 from users2 a2 where a2.姓名 not in (
    select a1.姓名 from users1 a1, users2 a2,users3 a3
    where a1.姓名=a2.姓名 and a1.姓名=a3.姓名 and a2.姓名=a3.姓名
    )
    union
    select a3.姓名 from users3 a3 where a3.姓名 not in (
    select a1.姓名 from users1 a1, users2 a2,users3 a3
    where a1.姓名=a2.姓名 and a1.姓名=a3.姓名 and a2.姓名=a3.姓名
    )
      

  3.   


    --这样可读性更好
    WITH CTE AS(
       select a1.姓名 as 'sss' from users1 a1, users2 a2,users3 a3
       where a1.姓名=a2.姓名 and a1.姓名=a3.姓名 and a2.姓名=a3.姓名
    )
    select 姓名 from users1 where 姓名 not in (select sss from cte)
    union
    select 姓名 from users2 where 姓名 not in (select sss from cte)
    union
    select 姓名 from users3 where 姓名 not in (select sss from cte)
      

  4.   


    --7楼“姓名”改为“身份证”
    --这样可读性更好
    WITH CTE AS(
       select a1.身份证 as 'sss' from users1 a1, users2 a2,users3 a3
       where a1.身份证=a2.身份证 and a1.身份证=a3.身份证 and a2.身份证=a3.身份证     --找到都存在的
    )
    select * from users1 where 身份证 not in (select sss from cte)
    union
    select * from users2 where 身份证 not in (select sss from cte)
    union
    select * from users3 where 身份证 not in (select sss from cte)
      

  5.   

    --如果身份证号码在每个表中是唯一的,用下面的方法就可以找出来不同时存在于各表的身份证号码
    with t as (select 身份证 from tb1 
             union all
               select 身份证 from tb2
             union all
               select 身份证 from tb3)
     select 身份证 from t group by 身份证 having COUNT(*)<3