• mysql 递归获取内容


    表结构

    1. CREATE TABLE `sys_permission` (
    2. `fn_code` varchar(50) NOT NULL COMMENT '功能权限id 功能权限编码',
    3. `fn_name` varchar(100) DEFAULT NULL COMMENT '功能权限名',
    4. `parent_id` varchar(11) DEFAULT NULL COMMENT '上级功能权限'
    5. PRIMARY KEY (`fn_code`) USING BTREE
    6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='权限表';

    子求父

    5.7版本

    1. <foreach collection="permissionList" separator=" union " item="fnCode" index="i">
    2. SELECT t2.fn_code, t2.`fn_name`,t2.fn_order
    3. FROM
    4. (
    5. SELECT
    6. @r${i} AS _id,
    7. (SELECT @r${i} := parent_id FROM sys_permission WHERE fn_code = _id) AS parent_id,
    8. @l${i} := @l${i} + 1 AS lvl
    9. FROM
    10. (SELECT @r${i} := #{fnCode}, @l${i} := 0) vars, sys_permission AS h
    11. WHERE @r${i} != 0
    12. ) t1
    13. JOIN sys_permission t2
    14. ON t1._id = t2.fn_code
    15. </foreach>
    16. 原始
    17. SELECT
    18. t2.fn_code,
    19. t2.`fn_name`,
    20. t2.fn_order
    21. FROM
    22. (
    23. SELECT
    24. @r AS _id,
    25. ( SELECT @r := parent_id FROM sys_permission WHERE fn_code = _id ) AS parent_id,
    26. @l := @l + 1 AS lvl
    27. FROM
    28. ( SELECT @r := '010101', @l := 0 ) vars,
    29. sys_permission AS h
    30. WHERE
    31. @r <> 0
    32. ) t1
    33. JOIN sys_permission t2 ON t1._id = t2.fn_code

    8.0版本

    1. WITH recursive temp AS (
    2. SELECT
    3. *
    4. FROM
    5. sys_permission p
    6. WHERE
    7. p.fn_code in
    8. <foreach collection="permissionList" open="(" close=")" item="fnCode" separator=",">
    9. #{fnCode}
    10. </foreach>
    11. UNION ALL
    12. SELECT
    13. *
    14. FROM
    15. sys_permission p,
    16. temp t
    17. WHERE
    18. t.parent_id = p.fn_code
    19. )
    20. SELECT
    21. *
    22. FROM
    23. temp

    父获子

    8.0版本

    1. WITH recursive temp AS (
    2. SELECT
    3. *
    4. FROM
    5. sys_permission p
    6. WHERE
    7. p.fn_code in
    8. <foreach collection="permissionList" open="(" close=")" item="fnCode" separator=",">
    9. #{fnCode}
    10. </foreach>
    11. UNION ALL
    12. SELECT
    13. *
    14. FROM
    15. sys_permission p,
    16. temp t
    17. WHERE
    18. t.fn_code = p.parent_id
    19. )
    20. SELECT
    21. *
    22. FROM
    23. temp

    5.7

    1. 根据一个父节点查询所有子节点(包含自身)
    2. SELECT
    3. au.fn_code
    4. FROM
    5. ( SELECT * FROM sys_permission WHERE parent_id IS NOT NULL ) au,
    6. ( SELECT @pid := '01' ) pd
    7. WHERE
    8. FIND_IN_SET( parent_id, @pid ) > 0
    9. AND @pid := concat( @pid, ',', fn_code ) UNION
    10. SELECT
    11. fn_code
    12. FROM
    13. sys_permission
    14. WHERE
    15. fn_code = '01';
    16. 根据多个父节点查询所有子节点(包含自身)
    17. SELECT
    18. au.fn_code
    19. FROM
    20. ( SELECT * FROM sys_permission WHERE parent_id IS NOT NULL ) au,
    21. ( SELECT @pid := '01,02' ) pd
    22. WHERE
    23. FIND_IN_SET( parent_id, @pid ) > 0
    24. AND @pid := concat( @pid, ',', fn_code ) UNION
    25. SELECT
    26. fn_code
    27. FROM
    28. sys_permission
    29. WHERE
    30. FIND_IN_SET( fn_code, @pid ) > 0;

  • 相关阅读:
    自动化测试基础篇:Selenium 框架设计(POM)
    在小公司编程是一种什么样的体验?
    IBM MQ MQCSP
    Javascript专项练习
    C练题笔记之:Leetcode-第 85 场双周赛---6157. 二进制字符串重新安排顺序需要的时间
    考研408,1000道精心打磨的计算机考研题,选它就对了!
    代码随想录算法训练营第二十九天| LeetCode491. 递增子序列、LeetCode46. 全排列、LeetCode47. 全排列 II
    图像数字化基础
    【Loadrunner】学习loadrunner——性能测试基础篇(一)
    0动态规划中等 LeetCode1014. 最佳观光组合
  • 原文地址:https://blog.csdn.net/Y_vocefenrir/article/details/125431970