create table 部门表
( 部门表 char(2) nut null primary key,
   部门名称 varchar(30) not null

create table 雇员表
( 雇员代号 char(4) not null primary key,
   雇员姓名 varchar(20) not null

create table 任职表 
( 雇员代号 char(4) not null,
   部门号 char(2) not null,
   开始时间 datetime not null,
   备注 varchar(1000) null,
   constraint pk_works primary key(雇员代号,部门号,开始时间),
   constraint fk_employees foreign key(雇员代号)
    referenses 雇员表(雇员代号)on delete cascade 
    constraint fk_employees foreign key(部门号)
     references 部门表(部门号))
如何获得每个部门都工作过的雇员名单 最好能解释一下 语言的逻辑意义

解决方案 »

  1.   

    select 雇员代号from (select distinct 雇员代号,部门号 from 任职表 )a
    having count(*)=(select count(*) from 部门表)
    /***
    任职表 每个员工对应的不同部门的个数总和和部门表里面的格式一样不就是都工作过的吗〉
      

  2.   

    select * from 雇员表 a
    where not exists (
       select 1 from  部门表 b
       where not exists (
          select 1 from 任职表
           where 部门号=b.部门号
           and 雇员代号=a.雇员代号
           )
       )--不好解释
      

  3.   

    因为有两个没有,所以需要两个not exists
      

  4.   

    slect 雇员姓名 from 雇员表 as a where 
    not exists (select b.部门号 from 部门表 as b where b.部门号 not in
                ( select c.部门号 from 任职表 as c 
                   where a.雇员代号=c.雇员代号
                )

    这样行不行