zoukankan      html  css  js  c++  java
  • Sql关联表,取出继承自己的所有数据和被自己继承的所有数据 Frida

    写sql题,这个sql是一个自关联表,有ID,ParentID,Status。其中Status=1表示有继承关系,请写sql取出继承自己的所有数据和被自己继承的所有数据。

    总结的一个方法,解决方法不是最优的,只是单纯解决了问题

    CREATE TABLE [dbo].[test] (
    	[id] [int] IDENTITY (1, 1) NOT NULL ,
    	[ParentID] [int] NOT NULL ,
    	[Status] [int] NOT NULL 
    ) ON [PRIMARY]
    GO
    
    CREATE  proc gg @id int
    as
        declare @t table(id int,parentid int, status int)
    -- 开始检测父节点
    insert @t select * from test where id = @id and status =1 
        while @@rowcount > 0 
            insert @t select a.* from Test as a inner join @t as b
            on a.id = b.ParentID and b.status =1 and a.id not in(select id from @t)
    delete from @t where id = @id
    --删除自身
    --开始检测子节点 这里浪费性能 写法需要改进  有空再议
        insert @t select * from test where id = @id
        while @@rowcount > 0 
            insert @t select a.* from Test as a inner join @t as b
            on a.ParentID = b.id and a.status =1 and a.id not in(select id from @t)
        delete from @t where id = @id
        select * from @t
    GO
    
  • 相关阅读:
    Linux基本权限管理
    Spring JMS
    消息中间件 ActiveMQ的简单使用
    Ionic slides 轮播图
    Spring 3 MVC and XML example
    Java 数组
    Java String类
    Java static 使用
    http://blog.csdn.net/huang_xw/article/details/7090173
    http://blog.chinaunix.net/uid-20577907-id-3519578.html
  • 原文地址:https://www.cnblogs.com/luckjun/p/2059417.html
Copyright © 2011-2022 走看看