我在员工和工作组之间有以下多对多关系:
employees table ----------------- id empgroups table --------------- employee_id workgroup_id workorders table ---------------- workgroup_id
我正在尝试编写SQL,根据员工所属的工作组列出员工的所有工作人员。
这是我的尝试:
SELECT wonum, workgroup_id
FROM workorders
INNER JOIN employees
ON workorders.employee_id = employee_id
INNER JOIN empgroups
ON employees.employee.id = empgroups.employee_id
WHERE employee_id = 2
我得到的错误是:
ERROR: schema "employees" does not exist
抱歉 - 员工的身份证号码不是employee.id
答案 0 :(得分:21)
这不是你要找的吗?
SELECT wonum, workgroup_id
FROM workorders
JOIN empgroups
ON empgroups.workgroup_id = workorders.workgroup_id
JOIN employees
ON employees.employee_id = empgroups.employee_id
WHERE employees.employee_id = 2
答案 1 :(得分:3)
SELECT w.wonum, w.workgroup_id
FROM workorders w
JOIN empgroups e USING (workgroup_id)
WHERE e.employee_id = 2
此查询根本不需要表employees
。 USING
缩短了这种情况下的语法。和表别名一样。
答案 2 :(得分:0)
请尝试使用此查询:
SELECT * FROM empgroups
INNER JOIN employees
ON empgroups.empId = employees.id
INNER JOIN workorders
ON empgroups.woId = workorders.id