三个表和成一张表 ~
table 1
id name sex
1 aaa s
2 aaa2 s2
table 2
id table1_id adress
1 1 ss
2 2 ss2
table 3
id table1_id old
1 1 12
例如想这样的表 第三个表只有其中的几个字段 合并为
table 4
id name sex adress old
1 aaa s ss 12
2 aaa2 s2 ss2 null
------解决方案--------------------select a.*,b.adress,c.old from table1 a left join table2 b on a.id=b.id left join table3 c on a.id=c.id
------解决方案--------------------table 1
id name sex
1 aaa s
2 aaa2 s2
table 2
id table1_id adress
1 1 ss
2 2 ss2
table 3
id table1_id old
1 1 12
------------------
select a.*,b.adress,c.old
from t1 a left join t2 b on a.id=b.table1_id
left join t3 c on a.id=c.table1_id
------解决方案--------------------select table1.id,name,sex,adress,old from table1 left join table2 on table1.id = table2.table1_id left join table3 on table1.id = table3.table1_id
------解决方案--------------------select a.*,b.adress,c.old from [table_1] a left join [table_2] b on a.id=b.table1_id left join [table_3] c on a.id=c.table1_id
------解决方案--------------------create table table1(id int,name varchar(10),sex varchar(10))
insert table1
select 1 , 'aaa ', 's '
union select 2 , 'aaa2 ', 's2 '
create table table2(id int,table1_id int,adress varchar(10))
insert table2
select 1 ,1, 'ss '
union select 2,2, 'ss2 '
create table table3(id int,table1_id int,old int)
insert table3
select 1 , 1 , 12
select a.*,b.adress,c.old from table1 a left join table2 b on a.id=b.table1_id left join table3 c on a.id=c.table1_id
drop table table1,table2,table3
------解决方案--------------------select a.*,b.adress,c.old
from table1 a left join table2 b on a.id=b.table1_id
left join table3 c on a.id=c.table1_id