zoukankan      html  css  js  c++  java
  • 茴香豆的n种写法之③——sql更新id的n种写法

    此解法来自博客园思想瞭望者开设之“sql精英群”对话。

    iceline(43417365) 11:36:59

     
    现在PATH的上下级关系是对了,我如何更新parentID

    thanks(305380844) 12:54:12
    做出来了,试试
    update test set parentid = b.parentidb
    from test ,(
    select a.id, a.parentid, a.path , b.id as parentidb, b.path as bpath
    from test a cross join test b
    where len(REPLACE(a.path, b.path, ''))=4) as b
    where test.id = b.id

    Tdf(79187675) 13:17:20 
    declare @TT table
        (
          id int
        , ParentId int
        , path varchar(100)
        )
    insert into @TT
            ( id , path )
        values
            ( 1 , 'aaa' ),
            ( 2 , 'aaa/aab' ),
            ( 3 , 'aaa/aac' ),
            ( 4 , 'aaa/aab/aaa' ),
            ( 5 , 'aaa/aac/aa' ) ;
    ;
    with    TT
              as ( select
                        *
                    from
                        @TT
                    where
                        charindex('/' , path) = 0
                       -- id in ( 2 , 3 )
                   union all
                   select
                        T1.id , TT.id , T1.path
                    from
                        @TT T1
                      , TT
                    where
                        len(TT.path) < len(T1.path)
                        and left(T1.path , len(TT.path)) = TT.path
                        and substring(T1.path , len(TT.path) + 2 , len(T1.path) - len(TT.path)) not like '%/%'
                 )
        select
                *
            from
                TT

    Kenny(27694100) 13:21:09
    update test set parentid = b.id
      from test ,(select * from test) b
        where left(a.path,len(b.path)) = b.path and len(a.path) = len(b.path)+4

  • 相关阅读:
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符删除
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
    Java实现 蓝桥杯VIP 算法训练 字符串编辑
  • 原文地址:https://www.cnblogs.com/thanks/p/2324286.html
Copyright © 2011-2022 走看看