错误号:1137无法重新打开表:'d'

时间:2019-03-04 08:12:15

标签: mysql

select employee_id, first_name, last_name, department_name, location_id
from employees as e, departments as d
where d.department_id = e.department_id and (location_id)
in 
(select location_id
from departments
where department_name = 'Finance')

2 个答案:

答案 0 :(得分:0)

您可以使用内部联接代替in子句

select employee_id
  , first_name
  , last_name
  , department_name
  , d.location_id
from employees as e
INNER JOIN departments as d ON d.department_id = e.department_id 
INNER JOIN ( select location_id
from departments
where department_name = 'Finance') t  ON t.location_id = d.location_id

出于可读性考虑,我们应该基于where显式加入sintax,而不是隐含sintax

答案 1 :(得分:0)

似乎您不需要子查询来过滤location_id

select employee_id, first_name, last_name, department_name, location_id 
from employees as e join departments as d on d.department_id = e.department_id 
where location_id in (select location_id from departments where department_name = 'Finance')

注意:最好使用显式连接而不是逗号分隔的连接