我正在尝试使用JOIN,但似乎我只获得了一半的结果。以下是表格和所需输出。
我没有收到任何错误
当我使用JOIN时,它只显示approver1
。所以我做的是:
SELECT tb1.*, tb2.name AS a1, tb3.name AS a2
FROM table1 tb1
LEFT JOIN approver tb2
ON tb1.approve1 = tb2.id
LEFT JOIN approver tb3
ON tb1.approver2 = tb2.id
非常感谢任何帮助或线索。
答案 0 :(得分:2)
您必须使用LEFT JOIN
来获取没有关联审批者的行。例如。 Dwade Curtis
没有关联的approver2
SELECT e.emp_name, a1.name as ap1, a2.name as ap2
FROM employee e
LEFT JOIN approver a1 ON (a1.id=e.approver1)
LEFT JOIN approver a2 ON (a2.id=e.approver2)
This是对SQL JOIN的良好直观解释
答案 1 :(得分:1)
Select empname, ap_1.name, ap_2.name from Employee
left join Approver as ap_a on (Employee.approver1 = ap_1.id)
left join Approver as ap_b on (Employee.approver2 = ap_2.id)