mysql多层查询

附件:city.sql - djmg
mysql查询某节点下层的所有元素节点:

select parentId, areaId, areaCode,areaName, level, center
	  from city_area
	  where find_in_set(areaId,
		 	(select GROUP_CONCAT(childrenIds) from (
			 	select (
			     select @childrenIds:=GROUP_CONCAT(areaId) from city_area where FIND_IN_SET(parentId, @childrenIds)) as childrenIds
--			     ,CEILING( LENGTH(@childrenIds) / 5 )
		        from (select @childrenIds:=?) var,city_area c where  @childrenIds is not null
			) A )
		)
--	and areaName like '%区%';

mysql查询某节点上层的所有元素节点:

select parentId, areaId, areaCode,areaName, level, center
	  from city_area
	  where find_in_set(areaId,
		 	(select GROUP_CONCAT(childrenIds) from (
			 	select (
			     select @childrenIds:=GROUP_CONCAT(parentId) from city_area where FIND_IN_SET(areaId, @childrenIds)) as childrenIds
--			     ,CEILING( LENGTH(@childrenIds) / 5 )
		        from (select @childrenIds:=?) var,city_area c where  @childrenIds is not null
			) A )
		)
--	and areaName like '%区%';

两个查询的不同点在于最内层的查询将父子Id位置对换了一下
(select @childrenIds:=GROUP_CONCAT(parentId) from city_area where FIND_IN_SET(areaId, @childrenIds)) as childrenIds
(select @childrenIds:=GROUP_CONCAT(areaId) from city_area where FIND_IN_SET(parentId, @childrenIds)) as childrenIds

问题:

  1. group_concat函数被截断的问题

mysql的 group_concat 函数默认返回1024个字节长度,超过长度的会被截断;
命令行下输入:
SET GLOBAL group_concat_max_len=1024000;
or SET SESSION group_concat_max_len=1024000;

  1. jpa原生sql':'转义问题

字符:在jpa原生sql会被看作是变量的前缀
@childrenIds:=? ==> @childrenIds\\:=?

posted @ 2021-02-08 16:56  不要摘树叶  阅读(552)  评论(0编辑  收藏  举报