zoukankan      html  css  js  c++  java
  • mysql 函数tree状

    // 子节点的查询

    CREATE DEFINER = `root`@`%` FUNCTION `getDeptChildList`(rootId BIGINT) RETURNS longtext DETERMINISTIC BEGIN DECLARE sTemp LONGTEXT; DECLARE sTempChd LONGTEXT; SET sTemp = ''; SET sTempChd =cast(rootId as CHAR); loop1:LOOP SELECT group_concat(id) INTO sTempChd FROM sys_dept where FIND_IN_SET(pid,sTempChd)>0; IF sTempChd is not null THEN SET sTemp = concat(sTemp,',',sTempChd); ELSE LEAVE loop1; END IF; END LOOP; SET sTemp = TRIM(',' FROM sTemp); RETURN sTemp; END;


    select A.* from sys_dept A inner join (select getDeptChildList('1067246875800000066') as idArr) TEMP where FIND_IN_SET(A.id, TEMP.idArr);

     
    CREATE FUNCTION `getFatherLst`(rootId INT)
    RETURNS varchar(1000)
    BEGIN
    DECLARE sTemp VARCHAR(1000);
    DECLARE sTempPrt VARCHAR(1000);
     
    SET sTemp = '';
    SET sTempPrt = cast(rootId as CHAR);
     
    loop1:LOOP
    SELECT group_concat(father_id) INTO sTempPrt FROM tbl_tree where FIND_IN_SET(id,sTempChd)>0;
    IF sTempPrt is not null THEN
    SET sTemp = concat(sTemp,',',sTempPrt);
    ELSE
    LEAVE loop1;
    END IF;
    END LOOP;
     
    SET sTemp = TRIM(',' FROM sTemp);
    RETURN sTemp;
    END


    select A.* from tbl_tree A inner join (select getFatherLst(15000) as idArr) TEMP where FIND_IN_SET(A.id, TEMP.idArr);
  • 相关阅读:
    原始字符串
    .Net Core 常见错误解决记录
    P1010 幂次方 P1022 计算器的改良
    P1036 选数
    广度优先遍历
    P4327 彼得潘框架
    链表
    标准库与标准模板库
    信息学竞赛打表犯规吗?
    对拍程序
  • 原文地址:https://www.cnblogs.com/chengyangyang/p/10489270.html
Copyright © 2011-2022 走看看