简体   繁体   中英

display all departments and all employees

if there :

(department) table: (id,name)

(employee) table : (id,dept_id,name)

how to show every department (id,name), then all employees (id,name) in this department under its department.

I'd like it as SQL statment

You need to use JOIN

I believe it's something like this:

SELECT department.id, department.name, employee.id, employee.name
FROM department
LEFT JOIN employee
ON department.id=employee.dept_id
ORDER BY department.id

Since all employees must be present under a particular department at any time, you can do a inner join on both the table with dept_id like

SELECT dept.id, dept.name, emp.id, emp.name
FROM department dept
JOIN employee emp
ON dept.id=emp.dept_id

Simply try this

SELECT D.ID,D.Name,E.ID,E.Name
FROM Department D Left JOIN Employee E ON E.dept_id = D.Id

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