【发布时间】:2017-08-07 07:53:33
【问题描述】:
我有这张桌子
tbl_emp
ID| name |
1 | a |
2 | b |
3 | c |
4 | d |
tbl_remit
ID| remit |
1 | 2012-01-01|
2 | 2013-01-01|
3 | 2012-05-01|
tbl_report
ID| report |
1 | 2012-01-01|
2 | 2013-01-01|
3 | 2012-05-01|
无论 tbl_remit 或 tbl_report 中是否有数据,我都需要将它们全部加入 tbl_emp。
这是我使用但失败的代码。
SELECT tbl_emp*, tbl_remit.remit, tbl_report.report from tbl_emp
left join tbl_emp.ID = tbl_remit.ID LEFT JOIN tbl_emp.ID = tbl_report.ID
我得到的桌子是
ID | remit | report |
1 | NULL | NULL |
2 | NULL | NULL |
3 | NULL | NULL |
4 | NULL | NULL |
我需要的桌子是
ID | remit | report |
1 |2012-01-01|2012-01-01|
2 |2013-01-01|2013-01-01|
3 |2012-05-01|2012-05-01|
4 | NULL | NULL |
【问题讨论】:
-
你加入的语法是假的。该查询真的有效吗?语法必须是
from <table> join <table2> on <condition> join <table3> on <condition> -
对不起,我忘了输入 ON 条件,我无法正确复制或记忆语法先生,我的意思是
SELECT tbl_emp*, tbl_remit.remit, tbl_report.report from tbl_emp left join tbl_emp.ID = tbl_remit.ID ON tbl_emp.PEN = tbl_remit.PEN LEFT JOIN tbl_emp.ID = tbl_report.ID ON tbl_emp.PEN = tbl_report.PEN