BEGIN DECLARE sChildList VARCHAR(4000); DECLARE sChildTemp VARCHAR(4000); 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 =…
定义一个函数 ) 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…
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…
[需求]某用户只能查看其自己信息及其下级信息,涉及通过该用户所在部门获取其下所有部门(多层)id集合. 步骤一:对数据库进行设置: set global log_bin_trust_function_creators=TRUE; 可以在Navicate点击工具栏的[工具]-> [命令行界面]执行以上语句:也可以在服务器上进入数据库执行. 步骤二:执行以下sql语句,生成sql方法. (1)需求一,获取父级及子级id: CREATE FUNCTION `getChildList`(`rootId`…
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 @…