DB2 递归查询

  表的结构是  
  origin     destination   distance  
  ---------------------------------------  
  Germany     New York     8000  
  Germany     Chicago     8700  
  Chicago               Austin                1300  
  New York             Houston            2100  
  Houston              Austin                300  
  New York            Chicago              950  
  Italy                    New York           10000  
  Ireland               Chicago              10700  
  Chicago              Toronto              400  
  New York            Toronto              350  
  Mexico                Houston             770  
   
  SQL语句是  
  WITH   path   (origin,   destination,   distance,  stops)  
  AS(  
        SELECT   origin,   destination,   distance,  0    
        FROM     flights 
        WHERE    origin='Germany'  
  UNION   ALL  
        SELECT   p.origin, f.destination, p.distance+f.distance,   p.stops+1  
        FROM     flights   f,   path   p  
        WHERE    p.destination=f.origin)  
   
  SELECT   origin,   destination,   distance,   stops   FROM   path    
   
  结果是  
  origin     destinat    distance             stops  
  -----------------------------------------------------------  
  Germany     New York    8000                   0  
  Germany     Chicago    8700                   0  
  Germany             Houston            10100                 1  
  Germany             Chicago             8950                   1  
  Germany             Toronto             8350                   1  
  Germany             Austin               10000                  1  
  Germany             Toronto             9100                   1  
  Germany             Austin               10400                  2  
  Germany             Austin               10250                  2  
  Germany            Toronto              9350                   2 


第一次执行结果:  
          第一个select语句产生的结果为  
          path:        origin                destination                 distance               stops  
                           Germany               New   York                  8000                   0  
                           Germany               Chicago                     8700                   0  
          第二个select语句执行的结果为      
          path:         origin                destination                 distance               stops  
                        Germany               Hunston                     8000+2100             1  
                        Germany               Chicago                     8000+950               1  
                        Germany               Toronto                     8000+350               1  
                        Germany               Austin                      8700+1300              1  
                        Germany               Toronto                     8700+400               1  
          union   后的结果为:  
          path:        origin                destination                 distance               stops  
                        Germany               New   York                  8000                      0  
                        Germany               Chicago                     8700                       0  
                        Germany               Hunston                     8000+2100            1  
                        Germany               Chicago                     8000+950               1  
                        Germany               Toronto                     8000+350               1  
                        Germany               Austin                      8700+1300               1  
                        Germany               Toronto                     8700+400               1  
   
  第二次递归调用结果:(path发生了改变,重新执行第二个select子句)  
          path:        origin                destination                 distance               stops  
                        Germany               New   York                  8000                        0  
                        Germany               Chicago                     8700                         0  
                        Germany               Hunston                     8000+2100              1  
                        Germany               Chicago                     8000+950                 1  
                        Germany               Toronto                     8000+350                  1  
                        Germany               Austin                      8700+1300                 1  
                        Germany               Toronto                     8700+400                  1  
                        Germany               Austin                      8700+2100+300          2  
                        Germany               Austin                      8000+950+1300          2  
                        Germany               Toronto                     8000+950+400          2 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值