而在sql中使用多个获取错误

时间:2013-09-10 12:55:03

标签: sql-server

我有这样的查询:

SELECT  em.Ename 
FROM    dbo.Email_tbl e 
JOIN dbo.EmployeeMaster_tbl em ON em.Ecode = e.ECode
WHERE e.plcid='25'

给了我这样的输出:

Ename
-------------------
Ahmed
Ahmed Elkhouli
akbar
Alaudddin

我有另一个这样的查询:

SELECT e.Ename 
from dbo.EmployeeMaster_tbl e 
WHERE e.gid not IN(SELECT did 
                   FROM dbo.Designation_tbl d 
                   WHERE d.Dname ='Driver'  )

我得到这样的输出:

Ename
--------------------------------------------------
Shaijomon John
Masum
Tausef Ahmad
Abdu Rahiman
Mohammad Shopan
Benzy P A
Mohammad Ali
Kauser Mohammad
Rasheed Pulakkuttathil
Lakshuman Perappan
Ahmed
Ahmed Elkhouli
akbar
Alaudddin 

我需要为第一个查询中不存在的行输出ename。

所以我尝试了这样的查询:

SELECT e.Ename 
from dbo.EmployeeMaster_tbl e 
WHERE e.gid not IN(SELECT did 
                   FROM dbo.Designation_tbl d 
                   WHERE d.Dname ='Driver') 
and not in (SELECT  em.Ename 
            FROM  dbo.Email_tbl e 
            JOIN dbo.EmployeeMaster_tbl em ON em.Ecode = e.ECode 
            WHERE e.plcid='25')
order by e.Ename  

但是我在关键字in附近收到了错误的语法错误。

2 个答案:

答案 0 :(得分:0)

SELECT e.Ename from dbo.EmployeeMaster_tbl e 
WHERE e.gid not IN 
(SELECT did 
FROM dbo.Designation_tbl d
 WHERE d.Dname ='Driver') 
 and e.name not in (SELECT  em.Ename 
 FROM  dbo.Email_tbl e 
JOIN dbo.EmployeeMaster_tbl em 
ON em.Ecode = e.ECode 
WHERE e.plcid='25')
order by e.Ename

答案 1 :(得分:0)

试试这个,你在条件

中缺少e.Ename
SELECT e.Ename from dbo.EmployeeMaster_tbl e 
 WHERE e.gid not IN(SELECT did FROM dbo.Designation_tbl d WHERE d.Dname ='Driver') 
 and e.Ename not in (SELECT  em.Ename FROM  dbo.Email_tbl e 
 JOIN dbo.EmployeeMaster_tbl em ON em.Ecode = e.ECode WHERE e.plcid='25')
 order by e.Ename