|
- create table flights ( source varchar2(2) ,
- destin varchar2(2),
- flight_time number ) ;
-
- insert into flights values ( 'A', 'B' ,1 );
- insert into flights values ( 'C', 'D' ,2 );
- insert into flights values ( 'B', 'C' ,1 );
- commit;
复制代码 A B 1
C D 2
B C 3
- WITH Reachable_From (Source, Destin, Flight_time) AS
- (
- SELECT Source, Destin, Flight_time
- FROM Flights
- UNION ALL
- SELECT a.Source, b.Destin,
- a.Flight_Time+b.Flight_time
- FROM Reachable_From a, Flights b
- WHERE a.Destin = b.Source
- )
- SELECT *
- FROM Reachable_From;
复制代码 A B 1
C D 2
B C 1
A C 2
B D 3
A D 4
把一张表看成两张表,
把WITH定义的当作第一张表,原表当作第2张表,
UNION ALL,
第一张表的第一个字段,第二张表的最后一个字段,
条件是:第一张表的最后一个字段=第二张表的第一个字段
|
|