表员工
emp_id emp_name emp_manager
1 Akshay 3
2 Santosh 3
3 Naveen 4
4 Prashanth 5
5 Punnet Null
获取与员工相关联的所有管理
查询
SELECT a.emp_name, b.emp_name AS Manager
FROM employee AS a
INNER JOIN employee AS b ON a.emp_manager = b.emp_id
emp_name Manager
Akshay Naveen
Santosh Naveen
Naveen Prashanth
Prashanth Punnet
Null经理没有显示。 如何编写查询以在查询结果中将Punnet和manager显示为Null?
答案 0 :(得分:1)
使用LEFT JOIN获取NULL值。
您的查询中的更正:
SELECT a.emp_name, b.emp_name AS Manager
FROM employee AS a
LEFT JOIN employee AS b ON a.emp_manager = b.emp_id
答案 1 :(得分:1)
改为使用LEFT JOIN:
SELECT a.emp_name, b.emp_name AS Manager
FROM employee AS a
LEFT JOIN employee AS b ON a.emp_manager = b.emp_id;
有关详情,请参阅MySQL Join Made Easy。