为什么我的嵌套查询语句不起作用?

时间:2013-04-16 22:12:52

标签: sql sql-server-2008 nested-queries

以下是我试图回答的提示:

编写嵌套语句,列出与Larry Smith工作相同的员工的名字和姓氏。

以下是员工表列:

EMPLOYEE(Emp_Num,Emp_Lname,Emp_Fname,Emp_Initial,Emp_HireDate,Job_Code)

以下是我的嵌套查询:

select emp_fname, emp_lname
from EMPLOYEE
where job_code =
(select job_code
from employee
where emp_fname = 'larry'
and emp_lname = 'smith');

为什么这不起作用?

1 个答案:

答案 0 :(得分:3)

您应该使用IN而不是=

select emp_fname, emp_lname
from EMPLOYEE
where job_code IN
  (select job_code
   from employee
   where emp_fname = 'larry'
     and emp_lname = 'smith');

原因是您正在将值与子查询返回的集合进行比较。

相关问题