[英]MySQL - Query three tables in one statement
I'm trying to run a query where it selects information using the following three tables and then combines the results in to one.我正在尝试运行一个查询,它使用以下三个表选择信息,然后将结果合并为一个。
staff_members职员
s_id | name |salary_id(fk)| dept_id (fk) |
1 | John | 3 | 2 |
2 | Mike | 3 | 5 |
3 | Jen | 3 | 1 |
4 | Claire | 3 | 3 |
salaries工资
id | salary |
1 | 28000 |
2 | 32000 |
3 | 34500 |
4 | 38000 |
bridge_team_staff bridge_team_staff
t_id (fk)| s_id (fk)|
2 | 2 |
3 | 1 |
4 | 2 |
1 | 3 |
2 | 4 |
The bridge resolves a many to many relationship with teams and managers as a manager can have more than one team and a team can have more than one manager.桥梁解决了与团队和经理的多对多关系,因为一个经理可以拥有多个团队,而一个团队可以拥有多个经理。
The query I want to run is to gather the manager's name, dept_id and the salary they're on based on their team_id in the bridge table.我想运行的查询是根据他们在桥接表中的 team_id 收集经理的姓名、dept_id 和他们的工资。
For example, if I query the name, dept_id and salary of all staff members who are on team_id 2 it would show as below例如,如果我查询 team_id 2 上所有员工的姓名、dept_id 和薪水,它将显示如下
name | dept_id | salary |
Mike | 2 | 34500 |
Claire | 3 | 34500 |
If I've understand correctly, you want a query like this:如果我理解正确,您需要这样的查询:
SELECT m.name, m.dept_id, s.salary
FROM bridge_team_staff b
JOIN staff_members m ON(b.s_id = m.s_id)
JOIN salaries s ON(m.salary_id = s.id)
WHERE b.t_id = ?
SELECT name, dept_id, salary
FROM staff_members JOIN
bridge_team_staff on s_id = s_id JOIN
salaries on salary_id = id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.