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.
  • 相关阅读:
    sed 命令编辑文本
    Iocomp控件教程之Pie Chart——饼状图控件
    《Go并发编程实战》第2版 紧跟Go的1.8版本号
    浅谈PHP数据结构之栈
    oracle11g导入dmp文件(根据用户)
    exp命令ORACLCE10G导出ORACLE11G的数据1455错误
    将war文件解压到指定目录
    JAVA包命名规范
    Eclipse中新建jsp文件访问页面时乱码问题
    网页编码就是那点事
  • 原文地址:https://www.cnblogs.com/imimjx/p/14389456.html
Copyright © 2011-2022 走看看