日期:2014-05-19  浏览次数:20527 次

EXISTS的问题
刚接触SQL不太会用EXISTS
这有道题怎样解:
某公司SQL   Server   2000一数据库中的Employees表用于存放所有员工的信息,Orders表用于存放所有的订单信息,Orders表有一外部键引用Employees表的主键,两表的关系如下图所示:
 
现在公司想统计一下最近一个月内没有任何销售订单的员工,则下列SQL语句中符合要求的有(   )。(选择一项)

a) SELECT   LastName,   FirstName  
FROM   Employees   AS   e
WHERE   EXISTS  
(SELECT   *   FROM   Orders  
  WHERE   Orders.EmployeeID=e.EmployeeID  
              AND   Orders.OrderDate   >   GetDate()-30)
b) SELECT   LastName,   FirstName  
FROM   Employees   AS   e
WHERE     (SELECT   *   FROM   Orders  
  WHERE   Orders.EmployeeID=e.EmployeeID  
              AND   Orders.OrderDate   <   GetDate()-30)   >   0
c) SELECT   LastName,   FirstName  
FROM   Employees   AS   e
WHERE (SELECT   *   FROM   Orders  
  WHERE   Orders.EmployeeID=e.EmployeeID  
              AND   Orders.OrderDate   >   GetDate()-30)   <   0
d) SELECT   LastName,   FirstName  
FROM   Employees   AS   e
WHERE   EXISTS  
(SELECT   *   FROM   Orders  
  WHERE   Orders.EmployeeID=e.EmployeeID  
              AND   Orders.OrderDate   <   GetDate()-30)

是不是应该选D?


------解决方案--------------------
SELECT LastName, FirstName
FROM Employees AS e
WHERE NOT EXISTS
(SELECT * FROM Orders
WHERE Orders.EmployeeID=e.EmployeeID
AND Orders.OrderDate > GetDate()-30)
------解决方案--------------------
應該用NOT EXISTS ,怎麼會答案全是用EXISTS 的。