现在有这样的两个表。结构如下所示
表一
编号 姓名 性别 年龄
A0001 张三 男 35
A0002 李四 男 46
A0003 王五 男 34
表2
编号 职务标识
A0001 领导
A0002 非领导
A0003 非领导
A0001 非领导
A0002 领导
A0003 群众注:编号为外键关联两个表,“领导标识”字段只有“领导”、“非领导”和“群众”三种状态。现在要求是,查询出来“职务标识”既是“领导”而且又是“非领导”的人员信息??这个怎么办呢??谢谢大家了~~!

解决方案 »

  1.   

    select a.*
    from 表一 a join 表二 b 
    on a.编号=b.编号 
    where exists(select * from 表2 where a.编号=编号 and 职务标识='领导')
    and exists(select * from 表2 where a.编号=编号 and 职务标识='非领导')
      

  2.   


    SELECT T.* FROM TB1 T ,
    (
    SELECT * FROM TB2 T WHERE 
    EXISTS(SELECT 1 FROM TB2 WHERE 编号=T.编号 AND 职务标识 ='领导')
    AND 
    EXISTS(SELECT 1 FROM TB2 WHERE 编号=T.编号 AND 职务标识 ='非领导')
    )AS T1
    WHERE T.编号=T1.编号
      

  3.   


    select * from 表一  a where exists(select * from 表二 where 编号=a.编号 and  职务标识 ='领导')
      and exists(select * from 表二 where 编号=a.编号 and  职务标识 ='非领导')
      

  4.   


    drop table tb2
    create table tb1(id varchar(10),name varchar(10),sex varchar(10),age int)
    insert tb1
    select 'A0001', '张三', '男', 35  union all
    select 'A0002', '李四', '男', 46  union all
    select 'A0003', '王五' ,'男' ,34 create table tb2(id1 varchar(10),bs varchar(10))
    insert tb2
    select 'A0001' ,'领导'  union all
    select 'A0002' ,'非领导'  union all
    select 'A0003' ,'非领导'  union all
    select 'A0001' ,'非领导'  union all
    select 'A0002', '领导'  union all
    select 'A0003', '群众' select * into #a from TB1 join tb2 on tb1.id =tb2.id1 
    select * from #a  a 
    where exists(select 1 from #a where id =a.id and name=a.name and a.bs in('领导','非领导'))
    order by ID
      

  5.   

    GROUP BY 编号 HAVING COUNT(1)>1
      

  6.   


    create table 表一(编号 varchar(10),name varchar(10),sex varchar(10),age int)
    insert 表一
    select 'A0001', '张三', '男', 35  union all
    select 'A0002', '李四', '男', 46  union all
    select 'A0003', '王五' ,'男' ,34 create table 表二(编号 varchar(10),职务标识 varchar(10))
    insert 表二
    select 'A0001' ,'领导'  union all
    select 'A0002' ,'非领导'  union all
    select 'A0003' ,'非领导'  union all
    select 'A0001' ,'非领导'  union all
    select 'A0002', '领导'  union all
    select 'A0003', '群众' 
    select distinct a.*
    from 表一 a join 表二 b 
    on a.编号=b.编号 
    where exists(select * from 表二 where a.编号=编号 and 职务标识='领导')
    and exists(select * from 表二 where a.编号=编号 and 职务标识='非领导')
    /*
    编号         name       sex        age
    ---------- ---------- ---------- -----------
    A0001      张三         男          35
    A0002      李四         男          46*/
      

  7.   


    create table tb1(id varchar(10),name varchar(10),sex varchar(10),age int)
    insert tb1
    select 'A0001', '张三', '男', 35  union all
    select 'A0002', '李四', '男', 46  union all
    select 'A0003', '王五' ,'男' ,34 create table tb2(编号 varchar(10) ,职务标号 varchar(20))
    insert into tb2
    select 'A0001' ,'领导' union all
    select 'A0002' ,'非领导' union all 
    select 'A0003','非领导' union all
    select 'A0001' ,'非领导'union all 
    select 'A0002' ,'领导' union all
    select 'A0003' ,'群众' 
    select * from tb2select * from tb1 where id in( select t.编号 from tb2 t where 职务标号='领导' or 职务标号='非领导' group by t.编号 having(count(1)>=2))drop table tb1
    drop table tb2
    /*
    A0001 张三 男 35
    A0002 李四 男 46*/