SQL与where子句连接

时间:2012-07-24 07:13:36

标签: sql join

有两个表 - 一个包含员工详细信息,另一个包含订单详细信息。

如何找到未在特定日期下订单的员工姓名?

我正在使用左外连接,我的代码就像这样

SELECT 
    emp.FirstName+' '+emp.LastName AS 'FullName', emp.employeeid 
FROM Employee emp
    INNER JOIN Orders oON emp.EmployeeID=o.EmployeeID
WHERE 
    o.OrderDate NOT IN  (SELECT o.OrderDate FROm Orders o WHERE o.OrderDate='4/4/1997')
GROUP BY 
    emp.FirstName+' '+emp.LastName, emp.employeeid

但问题是它会占据列表中的所有人员。

3 个答案:

答案 0 :(得分:3)

SELECT emp.FirstName+' '+emp.LastName AS 'FullName', emp.employeeid 
FROM Employee emp LEFT JOIN Orders o 
ON emp.EmployeeID=o.EmployeeID And o.OrderDate='4/4/1997'
where o.EmployeeID is null 
GROUP BY emp.FirstName+' '+emp.LastName, emp.employeeid

试试这个。应该管用。和评论答案,因为我无法在我当地测试...

答案 1 :(得分:1)

一种方法是使用不存在

SELECT  
    emp.FirstName+' '+emp.LastName AS 'FullName', emp.employeeid  
FROM Employee emp 
where not exists
    (select * from Orders o ON where emp.EmployeeID=o.EmployeeID 
        and  o.OrderDate='4/4/1997' 
    )

答案 2 :(得分:0)

SELECT FirstName + ' ' + LastName AS 'FullName', 
       employeeid 
FROM Employee
where EmployeeID not in (select EmployeeID 
                         from Orders 
                         where OrderDate = '1997-04-04')