在外部查询中显示department_name - sql

时间:2018-02-21 14:53:02

标签: mysql database mysql-workbench

我有这个查询

select last_name,job_id,department_id
from empl_demo
where (department_id) in 
(select department_id
from departments
where department_id=60);

我的问题是如何显示" department_name"从部门表?例如:

select last_name,job_id,department_id,department_name
from empl_demo, departments;

但是它符合我的条件

感谢

2 个答案:

答案 0 :(得分:0)

似乎department_id是两个表的共同属性。因此,请尝试使用JOINS,如:

select A.last_name, A.job_id, B.department_id, B.department_name
from empl_demo A join departments B
on A.department_id=B.department_id
where B.department_id=60;

如果您想使用子查询,请尝试:

select A.last_name, A.job_id, B.department_id, B.department_name
from (select * from empl_demo where department_id=60) A join 
(select * from departments where department_id=60) B
on A.department_id=B.department_id;

答案 1 :(得分:0)

经典INNER加入,你真的需要阅读一些基础知识SQL课程(https://www.w3schools.com/sql/),例如。

SELECT e_d.last_name,
   e_d.job_id,
  e_d.department_id,
  d.department_name
FROM empl_demo e_d
INNER JOIN departments d on d.department_id = e_d.department_id
where department_id=60