表A
ID name Address
1 Jerry GuanZhuo
表B
ID Name Address
1 Joy ShanHai表C
OrderID Name
1 Jerry
2 Joy因为系统有两个用户表和一个订单表设计的很不好的,但现在要查询出订单表中他们的地址结果为OrderID Name ID Address
1 Jerry 1 GuanZhuo
2 Joy 1 ShanHai谢谢
ID name Address
1 Jerry GuanZhuo
表B
ID Name Address
1 Joy ShanHai表C
OrderID Name
1 Jerry
2 Joy因为系统有两个用户表和一个订单表设计的很不好的,但现在要查询出订单表中他们的地址结果为OrderID Name ID Address
1 Jerry 1 GuanZhuo
2 Joy 1 ShanHai谢谢
from c left join (select * from a union all select * from b) t
on c.name = t.name
from 表C a,
(select name,address from 表A union all select name,address from 表B) b
where a.name=b.name
select c.*,
isnull(a.id,b.id) as id,
isnull(a.Address,b.Address ) as Address
from c left join a on c.name=a.name
left join b on c.name=b.name
from 表C a,
(select id,name,address from 表A union all select id,name,address from 表B) b
where a.name=b.name
insert into A values(1, 'Jerry', 'GuanZhuo' )
create table B(ID int,name varchar(10),Address varchar(10))
insert into B values(1, 'Joy', 'ShanHai ' )
create table c(OrderID int,Name varchar(10))
insert into c values(1, 'Jerry')
insert into c values(2, 'Joy')
goselect c.* , t.id , t.address
from c left join (select * from a union all select * from b) t
on c.name = t.namedrop table a,b,c/*
OrderID Name id address
----------- ---------- ----------- ----------
1 Jerry 1 GuanZhuo
2 Joy 1 ShanHai (2 行受影响)
*/