我有两个表(所有者和员工)和一个映射表(owner_employee)
我需要一个select语句,通过 employee_id 获取所有所有者员工,而不使用内部选择
示例:
SELECT * FROM employee e
[...Joins here...]
WHERE e.id = 1
应该返回
owner | employee
1 | 1
1 | 2
所有者表
id | title
----------
1 | a
2 | b
3 | c
员工表
id | title
----------
1 | a
2 | a
3 | b
4 | c
Owner_Employee
id_owner | id_employee
----------------------
1 | 1
1 | 2
2 | 3
3 | 4
这里内部选择的工作实现(可能正在工作,因为我从原始示例重写了它)
SELECT *
FROM owner_employee oe
JOIN owner o ON o.id = oe.id_owner
JOIN employee e ON e.id = oe.id_employee
WHERE o.id in
(SELECT id_owner
FROM owner_employee oe
WHERE oe.id_employee in (1))
答案 0 :(得分:0)
也许是这样的:
SELECT DISTINCT oe.*,o.*,e.*
FROM owner_employee AS z
JOIN owner_employee oe on oe.id_owner = z.id_owner
JOIN owner o ON o.id = oe.id_owner
JOIN employee e ON e.id = oe.id_employee
WHERE z.id_employee in (1);
答案 1 :(得分:0)
您提供的查询代码(不会返回您的文本要求的内容)与
相同SELECT *
FROM owner_employee oe
JOIN owner o ON o.id = oe.id_owner
JOIN employee e ON e.id = oe.id_employee
WHERE oe.id_employee = 1
但是您的文字要求的是"所有所有者员工都是employee_id"显然没有意义。由于您的数据,一个所有者可以拥有多个id_employee。
如果你想要"所有id_owners及其id_employees":
SELECT * FROM owner_employee
如果您想要"所有所有者的id_employees":
SELECT id_employee FROM owner_employee
或许你想要"所有的所有者,每个人都是他们的员工ID"?