如果有:
(部门)表:(id,name)
(员工)表:(id,dept_id,name)
如何显示每个部门(id,name),然后显示该部门下所有部门的所有员工(id,name)。
我希望它作为SQL语句
答案 0 :(得分:1)
您需要使用JOIN
我相信它是这样的:
SELECT department.id, department.name, employee.id, employee.name
FROM department
LEFT JOIN employee
ON department.id=employee.dept_id
ORDER BY department.id
答案 1 :(得分:1)
由于所有员工必须随时出现在特定部门下,因此您可以inner join
同时使用dept_id
SELECT dept.id, dept.name, emp.id, emp.name
FROM department dept
JOIN employee emp
ON dept.id=emp.dept_id
答案 2 :(得分:0)
只需尝试这个
SELECT D.ID,D.Name,E.ID,E.Name
FROM Department D Left JOIN Employee E ON E.dept_id = D.Id