1、有两个表employee和department,他们有一个相同项目为department_id。
现在要查询department中的全部信息,条件是department中的department_id必须是在employee中没有的。举个例子吧:employee的department_id:
1
4department的department_id:
1
2
3
4请问如何选择
department_id
2
3?
2、如何选择多达一个的department_id
比如说我的employee表中的department_id 有两个1,请问如何选择?谢谢各位!

解决方案 »

  1.   

    SELECT * FROM DEPT D WHERE NOT EXISTS (SELECT 1 FROM EMPLOYEE E WHERE D.DEPT_ID=E.DEPT_ID)
      

  2.   

    随便你在EMP表里有多少个DEPT_ID,结果是一样的。
      

  3.   

    就是要查询表employee中department_id不存在的department的department_id?
      

  4.   

    对第一题,很简单的:
    select * from deptment where id not in (select id from employee);
    第二题我也没有看懂的,楼主是不是考虑再表述清楚一点了
      

  5.   

    对第一题:
    select * from deptment where id not in (select id from employee);
    第二题我也没看太懂,lz再表述清楚点撒
      

  6.   

    select department_id from employee group by department_id having count(department_id)>1
    ?不明白你的意思
      

  7.   

    第一题用not in 或者exists都行,第二题是不是这么理解,你不想去除重复的记录!这个不要做处理的,直接select,不过需要以employee表主表,使用join连接!