阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL实现树形递归查询

123次阅读
没有评论

共计 2424 个字符,预计需要花费 7 分钟才能阅读完成。

最近在做项目迁移,Oracle 版本的迁到 MySQL 版本,遇到有些 Oracle 的函数,MySQL 并没有,所以就只好想自定义函数或者找到替换函数的方法进行改造。

Oracle 递归查询
oracle 实现递归查询的话,就可以使用 start with … connect by

connect by 递归查询基本语法是:
select 1 from 表格 start with … connect by prior id = pId

start with:表示以什么为根节点,不加限制可以写 1 =1,要以 id 为 123 的节点为根节点,就写为 start with id =123

connect by:connect by 是必须的,start with 有些情况是可以省略的,或者直接 start with 1= 1 不加限制

prior:prior 关键字可以放在等号的前面,也可以放在等号的后面,表示的意义是不一样的,比如 prior id = pid,就表示 pid 就是这条记录的根节点了

具体可以参考前一篇 Oracle 方面的文章:https://www.linuxidc.com/Linux/2019-03/157225.htm

Oracle 方面的实现
<select id=”listUnitInfo” resultType=”com.admin.system.unit.model.UnitModel” databaseId=”oracle”>
        select distinct u.unit_code,
              u.unit_name,
              u.unit_tel,
              u.para_unit_code
        from lzcity_approve_unit_info u
        start with 1 = 1
        <if test=”unitCode != null and unitCode !=””>
            and u.unit_code = #{unitCode}
        </if>
        <if test=”unitName!=null and unitName!=””>
            and u.unit_name like ‘%’|| #{unitName} ||’%’
        </if>
        connect by prior u.unit_code = u.para_unit_code
            and u.unit_code &lt;>u.para_unit_code
    </select>

Mysql 递归查询
 下面主要介绍 Mysql 方面的实现,Mysql 并没有提供类似函数,所以只能通过自定义函数实现,网上很多这种资料,不过已经不知道那篇是原创了,这篇博客写的不错,https://www.2cto.com/database/201209/152513.html,下面我也是用作者提供的方法实现自己的,先感谢作者的分享

这里借用作者提供的自定义函数,再加上 Find_in_set 函数 find_in_set(u.unit_code,getunitChildList(#{unitCode})),getunitChildList 是自定义函数
<select id=”listUnitInfo” resultType=”com.admin.system.unit.model.UnitModel” databaseId=”mysql”>
        select distinct u.unit_code,
              u.unit_name,
              u.unit_tel,
              u.para_unit_code
        from t_unit_info u
        <where>
            <if test=”unitCode != null and unitCode !=””>
                and find_in_set(u.unit_code,getunitChildList(#{unitCode}))
            </if>
            <if test=”unitName!=null and unitName!=””>
                and u.unit_name like concat(‘%’, #{unitName} ,’%’)
            </if>
        </where>
    </select>

getUnitChildList 自定义函数
DELIMITER $$

USE `gd_base`$$

DROP FUNCTION IF EXISTS `getUnitChildList`$$

CREATE DEFINER=`root`@`%` FUNCTION `getUnitChildList`(rootId INT) RETURNS VARCHAR(1000) CHARSET utf8
BEGIN
      DECLARE sChildList VARCHAR(1000);
      DECLARE sChildTemp VARCHAR(1000);
      SET sChildTemp =CAST(rootId AS CHAR);
      WHILE sChildTemp IS NOT NULL DO
        IF (sChildList IS NOT NULL) THEN
          SET sChildList = CONCAT(sChildList,’,’,sChildTemp);
    ELSE
      SET sChildList = CONCAT(sChildTemp);
    END IF;
        SELECT GROUP_CONCAT(unit_code) INTO sChildTemp FROM LZCITY_APPROVE_UNIT_INFO WHERE FIND_IN_SET(para_unit_code,sChildTemp)>0;
        END WHILE;
      RETURN sChildList;
END$$

DELIMITER ;

更多 Oracle 相关信息见 Oracle 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=12

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2424字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中