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 @…
定义一个函数 ) 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 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…
数据库记录如下: 现在要查询统计出每个'name'的'result'分别为'success'和'fail'的次数: 利用IF条件判断满足条件为1,不满足为0,再用SUM函数求和,最后通过'name'分组. SELECT NAME, , )) AS success, , )) AS fail FROM if_test GROUP BY `name` 结果如下:…
;with cte as( select id,ParentCategoryId from Category where id = 17 union all select a.id,a.ParentCategoryId from Category a join cte b on a.ParentCategoryId = b.id where a.id is not null) select * from cte…