求SQL语句,将多个表合成一个表,每个表的结构不一样。要过滤重复的字段和行
求SQL语句,将多个表合成一个表,每个表的结构不一样。要过滤重复的字段和行
如:
tb1
(
id int primary key,
name varchar(10),
title varchar(10),
)
tb2
(
id int ,
name varchar(10),
title varchar(10),
address varchar(10)
foreign key (id) references tb1(id)
)
tb3
(
id int,
name varchar(10),
tel varchar(10),
address varchar(10)
foreign key (id) references tb1(id)
)
------解决方案--------------------have a try
select a.id as id,
a.name as name,
a.title as title
b.address as address
c.tel as tel
into youur_new_table
from tb1 a,
(select distinct id, address from tb2) b,
(select distinct id, tel from tb3) c
where a.id = b.id
and a.id = c.id
------解决方案--------------------select distinct tab1.id,distinct tab1.name,distinct tab1.title,distinct tb2.name,
distinct tab2.title,distinct tab2.address,distinct tab2.name,distinct tab3.tel,
distinct tab3.address
from tab1 left join tab2 left join tab3