简体   繁体   中英

List the department names of all the employees working on a project

I am working with a mysql DB called company with Tables:

Employee-> |id_emp|fName|lName|id_Dept|

Department-> |id_Dept|dName|

Project-> |id_Proj|pName|id_Dept|status|budget|actBudget|

Emp_Proj-> |id_Emp|id_Proj|

I am still having a problem with how to run queries that work with the emp_proj join table.

The question is "List the dept names of all the employees working on a project.

SELECT d.dName
FROM department d
JOIN ?????
WHERE id_Emp = id_Proj ???

I'm not sure what to join b/c each employee has an id_Dept, and each employee working (or not working) on a project is shown in table emp_proj, so i'm not sure how to SELECT id_Emp working on a project and then compare that with the employee table to reference their id_Dept, and then output those dNames (dept names).

Can anyone help with this type of Query syntax? Tnx

SELECT DISTINCT dName FROM Department 
INNER JOIN Employee ON Employee.id_Dept = Department.id_Dept 
INNER JOIN Emp_Proj ON Employee.id_emp = Emp_Proj.id_Emp 
INNER JOIN Project ON Project.id_Proj = Emp_Proj.id_Proj 
WHERE Project.pName = 'Whotever'

This smells like a school project. If so please make sure you understand what this query means and why it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM