1.表结构 CREATE TABLE folder( id BIGINT(20) NOT NULL, parent_id BIGINT(20) DEFAULT NULL, PRIMARY KEY id ); 2.根据传入id查询所有子节点及其的id 创建函数: CREATE FUNCTION `getParList`(rootId BIGINT) RETURNS varchar(1000) BEGIN DECLARE sTemp VARCHAR(1000); DECLARE s
SELECT t3.college_code FROM ( SELECT t1.college_code, IF ( find_in_set( t1.parent_org_code, , ) AS ischild FROM ( ORDER BY parent_org_code, college_code ) t1, ( ' college_code ) t2 ) t3 WHERE t3.ischild != 0 表结构: t_college: id, college_code (机构编码), p
定义一个函数 ) CHARSET utf8 BEGIN ); ); SET sTemp = '$'; SET sTempChd =cast(rootId as CHAR); WHILE sTempChd is not null DO SET sTemp = concat(sTemp,',',sTempChd); ORDER BY sort, column_id; END WHILE; RETURN sTemp; END 使用: select t1.article_id, t1.sort,t1.s
SELECT T2.id, T2.name FROM ( SELECT @r AS _id, ,,@stop) as stop, (SELECT @r := p_id FROM goods_class WHERE id = _id) AS p_id, AS lvl FROM (, , ) vars, goods_class h ) T1 JOIN goods_class T2 ON T1._id = T2.id ORDER BY T1.lvl DESC
1.所示案例数据表结构设计如下所示: 2.案例数据如下所示: 3.mysql查询语句可以查询出父级目录信息: 注意:自己的数据表表名称,切记手动修改,字段名称(特别注意id,parent_id字段名称,不然肯定查询不出来的). SELECT T2.* FROM ( SELECT @r AS _id, (SELECT @r := parent_id FROM catelog WHERE id = _id) AS parent_id, @l := @l + AS lvl FROM (SELECT @
表结构和表数据就不公示了,查询的表user_role,主键是id,每条记录有parentid字段; 如下mysql查询函数即可实现根据一个节点查询所有的子节点,根据一个子节点查询所有的父节点.对于数据量较大的时候(我这里测试的1万条左右).查询效率非常慢.建议在java代码中进行处理. CREATE FUNCTION `getChildList`(rootId INT) ) BEGIN ); ); SET sChildTemp =cast(rootId as CHAR); WHILE sChil
--查询ID = '009'的所有父节点 ' ;WITH T AS ( SELECT ID , PID , NAME FROM TB WHERE ID = @ID UNION ALL SELECT A.ID , A.PID , A.NAME FROM TB AS A JOIN T AS B ON A.ID = B.PID ) SELECT * FROM T ORDER BY ID /* ID PID NAME ---- ---- ---------- 001 NULL 广东省 003 001 深
包含mysql 递归查询父节点 和子节点 mysql递归查询,查父集合,查子集合 查子集合 --drop FUNCTION `getChildList` CREATE FUNCTION `getChildList`(rootId varchar()) RETURNS varchar() BEGIN DECLARE str varchar(); DECLARE cid varchar(); SET str = '$'; SET cid = rootId; WHILE cid is not null
根据id查询父节点,具体需要修改的地方笔者已在注释中给大家作了注解 DELIMITER $$ USE `yjlc_platform`$$ -- getCompanyParent 为函数名 DROP FUNCTION IF EXISTS `getCompanyParent`$$ -- getCompanyParent 为函数名 rootId为参数,可以自定义:初学者可以不用更改 )) ) CHARSET utf8 BEGIN ); ); SET ptemp = '#'; SET ctemp = r
1.所有的子节点查询到树结构中所有父节点 RETURNS @Tree Table(PID )) as begin --DECLARE @ID VARCHAR() --SET @ID = ' ;with rolDepList as ( ) union all select a.PID,a.FID,a.Name from tbDepList a join rolDepList b on a.pid=b.fid ) Insert @Tree select distinct(PID),FID,Name
DELIMITER $$ USE `topsale`$$ DROP FUNCTION IF EXISTS `getShopIdByUserId`$$ CREATE DEFINER=`root`@`%` FUNCTION `getShopIdByUserId`(userId VARCHAR()) RETURNS VARCHAR() CHARSET utf8 ##定义返回值类型 varchar() BEGIN ##函数开始 DECLARE sTemp VARCHAR(); ##定义一个varchar