当前位置:网站首页 > SQL数据库编程 > 正文

oracle递归查询父级或子级转换成mysql的语句

前言:今天公司做项目,遇到原项目是做oracle的数据库,然后现在用的是mysql。所以在转换的过程中遇到了oracle的递归语句报错,原因是mysql没有这个高级的递归查询语句,所以上网查询转换方法,网上各种存储过程,中间表什么的,搞的我脑袋瓜疼(突然感觉oracle收费还是有点道理的)。

问题:

select id from tm_org start with id=“ e55f813b35015fc70003” connect by prior parent_id=id

此语句parent_id写在左边就是向上查询父级,写在右边就是查询所有子级。

解决方法:

1.使用mysql查询所有子级:

DELIMITER $$ CREATE FUNCTION getChildrenOrg(orgid INT) RETURNS VARCHAR(1000) BEGIN DECLARE oTemp VARCHAR(1000); DECLARE oTempChild VARCHAR(1000); SET oTemp = ''; SET oTempChild = CAST(orgid AS CHAR); WHILE oTempChild IS NOT NULL DO SET oTemp = CONCAT(oTemp, ',', oTempChild); SELECT GROUP_CONCAT(id) INTO oTempChild FROM tm_org WHERE FIND_IN_SET(parent_id, oTempChild) > 0; END WHILE; RETURN oTemp; END $$ SELECT * FROM tm_org WHERE FIND_IN_SET( id, getChildrenOrg (“e55f813b35015fc70003”) )

数据表名:tm_org,关键参与查询的字段,id、parent_id。

其中DELIMITER 定好结束符为"$$", 然后最后又定义为";", MYSQL的默认结束符为";". 

创建函数如果在终端命令行或者在客户端执行sql语句的时候,有时候分号默认结束了,但是我们不希望结束,所以一定要使用这个结尾符,今天我就在这里卡了好久,以为无法创建函数。切记啊!

函数创建截图:



调用函数查询所有子级:


2.使用mysql查询所有父级。

mysql查询所有父级就是可以直接写sql语句了。

SELECT * FROM ( SELECT @r AS _id, (SELECT @r := parent_id FROM tm_org WHERE id = _id) AS parent_id, @l := @l + 1 AS lvl FROM (SELECT @r := “40288ac3643f18fe00017”, @l := 0) vars, tm_org h WHERE @r <> 0) T1 JOIN tm_org T2 ON T1._id = T2.id ORDER BY id;

运行截图:


只需要把数据表的id写上去就行了。

结尾:附上今天我的修改并查询结果

代码:

SELECT DISTINCT t.*, t1.* FROM ( SELECT t.ID AS id, t.POSITION_CODE AS positionCode, t.POSITION_NAME AS positionName, t.POSITION_LEVEL AS positionLevel, t.ORG_ID AS orgId , t1.org_name AS orgName, t.PARENT_ID AS parentId, t2.position_name AS parentName, t.BUSINESS_GROUP AS businessGroup, t.HEADSTRING AS headString , t.ENABLE_STATUS AS enableStatus, t.UPDATE_NAME AS updateName, t.CREATE_NAME AS createName, t.UPDATE_DATE AS updateDate, t.CREATE_DATE AS createDate , t.EXT_CHAR_1 AS extChar1, t.EXT_CHAR_2 AS extChar2, t.EXT_CHAR_3 AS extChar3, t.EXT_CHAR_4 AS extChar4, t.EXT_CHAR_5 AS extChar5 , t.EXT_NUMBER_1 AS extNumber1, t.EXT_NUMBER_2 AS extNumber2, t.EXT_NUMBER_3 AS extNumber3, t.EXT_DATE_1 AS extDate1, t.EXT_DATE_2 AS extDate2 FROM tm_position t LEFT JOIN tm_org t1 ON t1.id = t.org_id LEFT JOIN tm_position t2 ON t2.id = t.parent_id WHERE 1 = 1 ) t LEFT JOIN ( SELECT p.id AS newId, o.org_name, o.id AS org_id, tmo.org_name AS parentOrgName, pp.position_code AS parentCode , trup.is_main AS isMain, trup.user_id, tu.username AS userName, tu.fullname AS fullName, ptu.fullname AS parentUserName , trup.id AS tmRPositionUserId FROM tm_position p LEFT JOIN tm_org o ON o.id = p.org_id LEFT JOIN tm_r_user_position trup ON trup.position_id = p.id LEFT JOIN tm_user tu ON tu.id = trup.user_id LEFT JOIN tm_position pp ON pp.id = p.parent_id LEFT JOIN tm_org tmo ON pp.org_id = tmo.id LEFT JOIN tm_r_user_position ptup ON pp.id = ptup.position_id LEFT JOIN tm_user ptu ON ptu.id = ptup.user_id ) t1 ON t1.newId = t.id WHERE 1 = 1 AND t.orgId IN ( SELECT id FROM ( SELECT @r AS _id , ( SELECT @r := parent_id FROM tm_org WHERE id = _id ) AS parent_id, @l := @l + 1 AS lvl FROM ( SELECT @r := "e55f813b35015fc70003", @l := 0 ) vars, tm_org h WHERE @r <> 0 ) T1 JOIN tm_org T2 ON T1._id = T2.id ) ORDER BY t.positionCode ASC;

运行截图:


到此这篇oracle递归查询父级或子级转换成mysql的语句的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • mysql主从复制设置从数据库延迟15分钟复制2024-11-28 15:00:06
  • centos Mysql使用mysqldump备份数据库2024-11-28 15:00:06
  • mysql创建用户、授权、修改密码、删除用户操作指令2024-11-28 15:00:06
  • 修改mysql的group_concat字符长度限制2024-11-28 15:00:06
  • SQLite 数据库资源文件下载2024-11-28 15:00:06
  • MAC 安装mysql全过程记录2024-11-28 15:00:06
  • NoSQL数据库(二)05-Redis数据类型——有序集合类型之介绍、命令-增加元素、获得元素的分数、获得排名在某个范围的元素列表、获得指定分数范围的元素、增加某个元素的分数2024-11-28 15:00:06
  • Node学习(六)042-SQL语句之node中的mysql模块——简单的查询 & conn.query执行其他SQL之err-错误信息、result-查询结果、fields-查询的字段信息,一般不用2024-11-28 15:00:06
  • Node学习(六)041-SQL语句之node中的mysql模块——Node中使用MySQL模块共5个步骤之加载MySQL模块、创建MySQL连接对象、连接MySQL服务器、执行SQL语句、关闭连接2024-11-28 15:00:06
  • Node学习(六)02-SQL语句——select查询、where筛选sql、like模糊查询通配符、order by查询结果排序、limit 用来限制查询结果的起始点和长度2024-11-28 15:00:06
  • 全屏图片