递归查询

发布于 2024-10-23 23:06:28 字数 1732 浏览 0 评论 0原文

我想得到关于我想做的递归查询的帮助。 我在 DB2 中创建了这个表:

connect reset;
connect to sample;
DROP TABLE FLIGHTS;

CREATE TABLE FLIGHTS
 (START         VARCHAR(16)     NOT NULL,
  DESTINATION   VARCHAR(16)     NOT NULL,
  DISTANCE  BIGINT          NOT NULL    
 );
insert into FLIGHTS values ('Dublin','Bhogrol',5340);
insert into FLIGHTS values ('Dublin','Smallville',5500);
insert into FLIGHTS values ('Smallville','Seattle',1300);
insert into FLIGHTS values ('Smallville','Clacton',6700);
insert into FLIGHTS values ('Bhogrol','Moscow',2320);
insert into FLIGHTS values ('Moscow','Seattle',3600);
insert into FLIGHTS values ('Bhogrol','Smallville',2950);
insert into FLIGHTS values ('Rome','Bhogrol',720);
insert into FLIGHTS values ('Clacton','Moscow',6700);
insert into FLIGHTS values ('Rome','Smallville',3050);
insert into FLIGHTS values ('Schippol','Smallville',8990);
insert into FLIGHTS values ('Seattle','Schippol',7840);
insert into FLIGHTS values ('Bhogrol','Clacton',1300);
insert into FLIGHTS values ('Bilbao','Moscow',1270);
insert into FLIGHTS values ('Smallville','Schippol',8990);

我想查找从都柏林出发且少于 8 次的所有转机航班 停止。查询的输出应该是具有以下 3 个字段的一组行 格式:[路线,距离,停靠点]其中路线字段是由 从都柏林出发的每条连接路径所访问的机场。这是机场名称的序列 用“-”字符分隔。但不要显示整个机场名称,只需显示第一个 三个字符,使每个字符串看起来像 例如,Dub>Sch>Dub>Sma>Sch>Dub>Sma>Cla>Mos。 属性“route”的类型应为 VARCHAR(40)。

我想这样开始:

WITH path (start, destination, distance, stops)
AS(
  SELECT f.start, f.destination, f.distance, 0
  FROM flights f
  WHERE start = 'Dublin'
  UNION ALL
  SELECT p.start, f.destination,
  p.distance + f.distance, p.stops+1
  FROM flights f, path p
  WHERE p.destination = f.start AND p.stops < 8 
)
SELECT start, destination, distance, stops 
FROM path;

这是正确的吗?如果是,那我该怎么办?

i would like your help about a recursive query which I want to do.
I created this table in DB2:

connect reset;
connect to sample;
DROP TABLE FLIGHTS;

CREATE TABLE FLIGHTS
 (START         VARCHAR(16)     NOT NULL,
  DESTINATION   VARCHAR(16)     NOT NULL,
  DISTANCE  BIGINT          NOT NULL    
 );
insert into FLIGHTS values ('Dublin','Bhogrol',5340);
insert into FLIGHTS values ('Dublin','Smallville',5500);
insert into FLIGHTS values ('Smallville','Seattle',1300);
insert into FLIGHTS values ('Smallville','Clacton',6700);
insert into FLIGHTS values ('Bhogrol','Moscow',2320);
insert into FLIGHTS values ('Moscow','Seattle',3600);
insert into FLIGHTS values ('Bhogrol','Smallville',2950);
insert into FLIGHTS values ('Rome','Bhogrol',720);
insert into FLIGHTS values ('Clacton','Moscow',6700);
insert into FLIGHTS values ('Rome','Smallville',3050);
insert into FLIGHTS values ('Schippol','Smallville',8990);
insert into FLIGHTS values ('Seattle','Schippol',7840);
insert into FLIGHTS values ('Bhogrol','Clacton',1300);
insert into FLIGHTS values ('Bilbao','Moscow',1270);
insert into FLIGHTS values ('Smallville','Schippol',8990);

I want to find all connected flights from Dublin with less than eight
stops. The output from the query should be a set of rows with the following 3-field
format: [ route, distance, stops ] where the route field is a string built up from the
airports visited in each connected path from Dublin. It is a sequence of airport names
separated by the '-' character. But instead of the whole airport name, just show the first
three characters, so that each string looks like
Dub>Sch>Dub>Sma>Sch>Dub>Sma>Cla>Mos, for example.
The type of attribute 'route' should be VARCHAR(40).

I thought to start in this way:

WITH path (start, destination, distance, stops)
AS(
  SELECT f.start, f.destination, f.distance, 0
  FROM flights f
  WHERE start = 'Dublin'
  UNION ALL
  SELECT p.start, f.destination,
  p.distance + f.distance, p.stops+1
  FROM flights f, path p
  WHERE p.destination = f.start AND p.stops < 8 
)
SELECT start, destination, distance, stops 
FROM path;

Is it correct?If yes, what can i do then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨死的猪 2024-10-30 23:06:28

我想你正在寻找这样的东西:

WITH path (route, start, destination, distance, stops)
AS
(
  SELECT substr(f.start,1,3) || ' > '|| substr(f.destination,1,3) as route,
         f.start, 
         f.destination,
         f.distance, 
         0
  FROM flights f
  WHERE start = 'Dublin'
  UNION ALL
  SELECT p.route || ' > ' || substr(f.destination,1,3) as route,
         f.start,
         f.destination,
         p.distance + f.distance, 
         p.stops + 1
  FROM flights f
    JOIN path p ON p.destination = f.start AND p.stops < 8 
)
SELECT route, distance, stops 
FROM path;

I think you are looking for something like this:

WITH path (route, start, destination, distance, stops)
AS
(
  SELECT substr(f.start,1,3) || ' > '|| substr(f.destination,1,3) as route,
         f.start, 
         f.destination,
         f.distance, 
         0
  FROM flights f
  WHERE start = 'Dublin'
  UNION ALL
  SELECT p.route || ' > ' || substr(f.destination,1,3) as route,
         f.start,
         f.destination,
         p.distance + f.distance, 
         p.stops + 1
  FROM flights f
    JOIN path p ON p.destination = f.start AND p.stops < 8 
)
SELECT route, distance, stops 
FROM path;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文