zoukankan      html  css  js  c++  java
  • SQL 递归查询

    CREATE TABLE [ptable](
     [id] [int] NULL,
     [pid] [int] NULL,
     [name] [nchar](10)
    )
    GO
    INSERT INTO ptable VALUES(1,0,'a')
    INSERT INTO ptable VALUES(2,0,'b')
    INSERT INTO ptable VALUES(3,1,'c')
    INSERT INTO ptable VALUES(4,1,'d')
    INSERT INTO ptable VALUES(5,2,'e')
    INSERT INTO ptable VALUES(6,3,'f')
    INSERT INTO ptable VALUES(7,3,'g')
    INSERT INTO ptable VALUES(8,4,'h')
    GO

    --查询出1结点的所有子结点
    with tmp as(select * from ptable where id = 1
     union all select ptable.* from tmp, ptable where tmp.id = ptable.pid
    )
    select * from tmp 

    --查询出8结点的所有父结点
    with tmp as(select * from ptable where id = 8
     union all select ptable.* from tmp, ptable where tmp.pid = ptable.id
    )
    select * from tmp;

    --递归删除1结点和所有子结点的语句:
    with tmp as(select * from ptable where id = 1
       union all select ptable.* from tmp, ptable where tmp.id = ptable.pid
    )
    delete from ptable where exists (select id from tmp where tmp.id = ptable.id) 
  • 相关阅读:
    生物神经网络和人工神经网络浅谈
    卷积神经网络
    DOM进阶之HTMl属性操作(对象属性)
    01 selenium基本使用补充
    01 selenium基本使用
    day4笔记
    03 获取豆瓣电影top250
    02 爬取视频
    day3笔记
    01 requests基本使用
  • 原文地址:https://www.cnblogs.com/pato/p/2427210.html
Copyright © 2011-2022 走看看