Menu Close

MariaDB之with as和update用法

当使用 update 更新数据并使用 with as 语句:

UPDATE sc_accdep x
   SET x.NamePath = (WITH RECURSIVE depinfo(accdepid,parentid,accdepname,lev,idpath,namepath)
                       AS (SELECT a.accdepid,
                                  a.parentid,
                                  a.accdepname,
                                  1 AS lev,
                                  CONCAT('/', Cast(a.accdepid AS VARCHAR(4000)),'/') AS idpath,
                                  CONCAT('/', a.accdepname,'/') AS namepath
                             FROM sc_accdep a
                            WHERE a.parentid = -1
                              AND a.epid = 1
                            UNION ALL
                           SELECT b.accdepid,
                                  b.parentid,
                                  b.accdepname,
                                  x.lev + 1,
                                  CONCAT(x.idpath,Cast(b.accdepid AS VARCHAR(4000)),'/') as idpath,
                                  CONCAT(x.namepath,b.accdepname,'/') AS namepath
                             FROM depinfo x
                            INNER JOIN (SELECT *
                                          FROM sc_accdep y
                                         WHERE y.epid = 1
                                           AND y.parentid <> -1
                                       ) b ON x.accdepid = b.parentid)
                   SELECT substring(y.namepath, 1, char_length(y.namepath) - 1)
                     FROM depinfo y
                    WHERE x.AccDepID = y.accdepid)
 WHERE x.EpID = 1;