我有 2 张表员工和部门
Table employee columns
empid
name
salary
location
Table department columns
deptno
deptname
empid
mangerid
现在我想检索带有名称的员工和经理列表。我试过了,但我无法找回
要为其添加一些亮点,您可以尝试以下操作:) 您可以按经理结果删除GROUP_CONCAT
普通GROUP_BY
员工。
参考:
样品表
电磁脉冲
ID NAME
1 JOHN
2 JIM
3 KATE
4 TIM
5 JANE
部门
ID EID MID
11 2 1
12 3 2
12 4 1
12 5 2
查询GROUP_CONCAT
SELECT M.NAME AS Manager, GROUP_CONCAT(E.NAME,' ') AS Employee
FROM EMP E
JOIN DEPT D
ON E.ID = D.EID
JOIN EMP M
ON D.MID = M.ID
GROUP BY M.NAME
结果:
MANAGER EMPLOYEE
JIM KATE ,JANE
JOHN TIM ,JIM
GROUP_CONCAT
不带and的查询GROUP_BY
SELECT M.NAME AS Manager, E.NAME AS Employee
FROM EMP E
JOIN DEPT D
ON E.ID = D.EID
JOIN EMP M
ON D.MID = M.ID
GROUP_CONCAT
不带and的查询结果GROUP_BY
MANAGER EMPLOYEE
JOHN JIM
JOHN TIM
JIM KATE
JIM JANE
尝试select emp1.name, emp2.name from employee emp1, employee emp2, department dept where emp1.empid=dept.empid and emp2.empid=dept.managerid