Oracle DB只是将两个表并排组合

时间:2015-05-26 12:30:45

标签: sql oracle

1:表国家

ID NAME   
1  USA  
2  BRITAIN

2:表人物

P_ID        P_NAME  
4           JOHN  
5           MONTY

结果表应该

ID   NAME        P_ID     P_NAME  
1    USA         4       JOHN  
2    BRITAIN     5       MONTY

只是并排合并。可能吗?喜欢TABLE1 | TABLE2

4 个答案:

答案 0 :(得分:5)

示例

with x as (
select id, name, rownum rn from country
order by id),
y as(
select p_id, p_name, rownum rn from people
order by p_id)
select id,name,p_id,p_name from x
left join y on y.rn = x.rn
union  
select id,name,p_id,p_name from y
left join x on y.rn = x.rn

答案 1 :(得分:3)

如果我很好理解你的问题,这可行:

SELECT * 
FROM (SELECT T1.*,ROWNUM AS R FROM COUNTRY T1) V1, 
    (SELECT T2.*,ROWNUM AS R FROM PEOPLE T2) V2
WHERE V1.R = V2.R(+)

但你需要知道桌子更大,我担心你不会得到可靠的结果

答案 2 :(得分:0)

这应该有用..

   select c.id,c.name, p.p_id, p.p_name from country c inner join people p on c.id = p.p_id;

答案 3 :(得分:0)

试试这个:

SELECT * FROM COUNTRY C
INNER JOIN PEOPLE P ON C.ID = P.P_ID;

更新回答:

Select * from #country as c , #people as p where p_id =4 and id=1 OR id = 2 and p_id=5