日期:2014-05-17  浏览次数:20768 次

求助 一个多表查询
A表 Person
personid personname


B表 Car
carid carname


C表 Conn(连接表)
personid carid 



查询后显示结果
personid personname carid carname 


注意事项:一个person可以对应多辆车 也就是结果中carid是没有重复的,personid可以重复

------解决方案--------------------
SQL code

with Person(personid,
personname) as
 (select 1, 'p1' from dual),
Car(carid,
carname) as
 (select 1, 'c1'
    from dual
  union
  select 2, 'c2' from dual),
Conn(personid,
carid) as
 (select 1, 1
    from dual
  union
  select 1, 2 from dual)
select p.personid, p.personname, c.carid, c.carname
  from person p, conn con, car c
 where p.personid = con.personid(+) and c.carid = con.carid

  PERSONID PE      CARID CA
---------- -- ---------- --
         1 p1          1 c1
         1 p1          2 c2