关于oracle多表联合查询的语句如何写?

2024-11-01 11:15:29
推荐回答(5个)
回答1:

我给猛尺你写一个,保证能用,验证枝敬高能用的话请采纳稿销!!!

select all_.bh,all_.name,a.sl,b.sl1,c.sl2,c.sl3 from
(
select bh,name from a
union
select bh,name from b
union
select bh,name from c
) all_
left outer join a on all_.bh = a.bh and all_.name = a.name
left outer join b on all_.bh = b.bh and all_.name = b.name
left outer join c on all_.bh = c.bh and all_.name = c.name

回答2:

当数据告芦胡记录条数不一致袜拦时,使用左联接查询哗盯:
select A.bh, A.name , sl, sl1,sl2,sl3
from A
left join B on A.bh= B.bh
left join C on A.bh= C.bh

回答3:

select a.bn,a.name,a.s1,b.s2,c.s3
from a,b,c
where a.bh=b.bh and a.bh=c.bh

回答4:

select * from A
union all
select * from B
union all
select * from C

回答5:

Try this:

SELECT t.bh, t.name, max(a.sl), max(b.sl1), max(c.sl2), max(c.sl3)
from (SELECT DISTINCT bh, name from a
UNION SELECT DISTINCT bh, name from b
UNION SELECT DISTINCT bh, name from c) t
LEFT JOIN a ON t.bh = a.bh and t.name = a.name
LEFT JOIN b ON t.bh = b.bh and t.name = b.name
LEFT JOIN c ON t.bh = c.bh and t.name = c.name
GROUP BY t.bh, t.name
ORDER BY t.bh