如何在mysql中从2个表中获取数据并左键连接到1个表中
我有这张桌子如何在mysql中从2个表中获取数据并左键连接到1个表中,mysql,join,left-join,Mysql,Join,Left Join,我有这张桌子 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-
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_汇款或tbl_报告中是否有数据,我都需要在tbl_emp中加入所有3个
下面是我使用但失败的代码
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 |
可以通过使用内部联接连接其他两个表来实现这一点。并将结果表与emp表联接。为此,可以使用子查询
select * from tbl_emp x left join (
select a.id as id, a.remit as remit,b.report as report from
tbl_remita,tbl_report b where a.id=b.id) y on x.id = y.id
希望能有所帮助。您的join语法为false。那个查询真的有效吗?语法必须是
from join on join on
对不起,我忘了键入on条件,我无法正确复制或记忆语法,先生,我的意思是SELECT tbl_emp*,tbl_remote.remote,tbl_report.report from tbl_emp left join tbl_emp.ID=tbl_emp.PEN=tbl_remit.PEN left join tbl_emp.ID=tbl_report.ID ON tbl_emp.PEN=tbl_report.PEN