zoukankan      html  css  js  c++  java
  • 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\:=?

    本文如果存在有问题请让我知道. 本文如果对你有帮助请让我知道. Created by Jmola.
  • 相关阅读:
    【原】React操作表单
    【原】使用webpack打包的后,公共请求路径的配置问题
    【原】React中,map出来的元素添加事件无法使用
    codeforces 1288C. Two Arrays(dp)
    Educational Codeforces Round 80 A-E简要题解
    HDU 2586 ( LCA/tarjan算法模板)
    POJ 1330(LCA/倍增法模板)
    POJ 3368 (ST表)
    POJ 3264 Balanced Lineup(ST模板)
    codeforces 1285E. Delete a Segment
  • 原文地址:https://www.cnblogs.com/imimjx/p/14389456.html
Copyright © 2011-2022 走看看