zoukankan      html  css  js  c++  java
  • WITH RECURSIVE 递归

    1 计算1到100的累加的结果。
    2 WITH RECURSIVE t(n) AS (
    3     VALUES (1)
    4   UNION ALL
    5     SELECT n+1 FROM t WHERE n < 100
    6 )
    7 SELECT sum(n) FROM t;

    sum

    5050

    =======================================

     1 /*
     2 https://www.yiibai.com/html/postgresql/2013/080565.html
     3 create table company (
     4     id int ,
     5     name varchar (60),
     6     age int ,
     7     address varchar (30),
     8     salary int
     9 );
    10 insert into company values(1,'Paul',32,'California',20000);
    11 insert into company values(2,'Allen',25, 'Texas',15000);
    12 insert into company values(3,'Teddy',23,'Norway',20000);
    13 insert into company values(4,'Mark',25,'Rich-Mond',65000);
    14 insert into company values(5,'David',27,'Texas',85000);
    15 insert into company values(6,'Kim',22,'South-Hall',45000);
    16 insert into company values(7,'James',24,'Houston',10000);
    17 */
    18  
    19 --递归WITH
    20 --找到小于20000的薪金总和
    21  
    22 WITH RECURSIVE t(n) AS (
    23         VALUES (0)
    24         UNION ALL
    25         SELECT salary FROM company WHERE salary < 20000
    26 )
    27 SELECT SUM(n) FROM t
    28  
    29 --删除指定的公司行,RETURNING子句返回它们的内容;然后主查询读取输出,并将其插入到COMPANY1 表:
    30  
    31 CREATE TABLE COMPANY1(
    32    ID INT PRIMARY KEY     NOT NULL,
    33    NAME           TEXT    NOT NULL,
    34    AGE            INT     NOT NULL,
    35    ADDRESS        CHAR(50),
    36    SALARY         REAL
    37 );
    38 WITH moved_rows AS (
    39     DELETE FROM COMPANY
    40     WHERE
    41         SALARY >= 30000
    42     RETURNING *
    43 )
    44 INSERT INTO COMPANY1 (SELECT * FROM moved_rows);
    45  
    46 SELECT * FROM COMPANY;
    47 SELECT * FROM COMPANY1;

    ============================================================================

    定义下面这样的表,存储每个区域(省、市、区)的id,名字及上级区域的pid

     1 create table tb(id varchar(3) , pid varchar(3) , name varchar(10)); 
     2  
     3 insert into tb values('002' , 0 , '浙江省'); 
     4 insert into tb values('001' , 0 , '广东省'); 
     5 insert into tb values('003' , '002' , '衢州市');  
     6 insert into tb values('004' , '002' , '杭州市') ; 
     7 insert into tb values('005' , '002' , '湖州市');  
     8 insert into tb values('006' , '002' , '嘉兴市') ; 
     9 insert into tb values('007' , '002' , '宁波市');  
    10 insert into tb values('008' , '002' , '绍兴市') ; 
    11 insert into tb values('009' , '002' , '台州市');  
    12 insert into tb values('010' , '002' , '温州市') ; 
    13 insert into tb values('011' , '002' , '丽水市');  
    14 insert into tb values('012' , '002' , '金华市') ; 
    15 insert into tb values('013' , '002' , '舟山市');  
    16 insert into tb values('014' , '004' , '上城区') ; 
    17 insert into tb values('015' , '004' , '下城区');  
    18 insert into tb values('016' , '004' , '拱墅区') ; 
    19 insert into tb values('017' , '004' , '余杭区') ; 
    20 insert into tb values('018' , '011' , '金东区') ; 
    21 insert into tb values('019' , '001' , '广州市') ; 
    22 insert into tb values('020' , '001' , '深圳市') ;

    需要查出某个省,比如浙江省,管辖的所有市及市辖地区

    with RECURSIVE cte as
    (
    select a.id,cast(a.name as varchar(100)) from tb a where id='002'
    union all 
    select k.id,cast(c.name||'>'||k.name as varchar(100)) as name  from tb k inner join cte c on c.id = k.pid
    )select id,name from cte ;

    to:https://blog.csdn.net/dufemt/article/details/80773394

  • 相关阅读:
    python自定义编写有关用户登录注册程序代码
    项目经理多年的经验之谈
    Linux虚拟机克隆后,启动系统发现网卡无法启动
    mysql出现服务器异常后,重启服务器后无法开启数据库处理方法
    计算机毕业四年,我都做了什么?
    个人Blog(采用Django+uwsgi+nginx)里面包含很多技术文章
    Delphi to C# Equivalancesdelphi和C#类似的地方
    Javascript浏览器关于scrollLeft,scrollTop的兼容性
    Mozilla Firefox15怎么样才能把标签页弄到下面去,就和360的一样,Mozilla Firefox15没有取消标签置顶这个选项……
    Delphi过程函数传递参数的几种方式
  • 原文地址:https://www.cnblogs.com/shenjiangwei/p/14068045.html
Copyright © 2011-2022 走看看