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.
  • 相关阅读:
    5分钟GET我使用Github 5 年总结的这些骚操作!
    这个项目可以让你在几分钟快速了解某个编程语言
    vulnstack靶机实战01
    PHP代码审计04之strpos函数使用不当
    【T1543.003】利用 ACL 隐藏恶意 Windows 服务
    Baolu CSV Data Set Config
    两种方式简单免杀ew
    mssql dba注入点写shell
    jeecms写shell的两种方法
    三种方式获取SSMS连接密码
  • 原文地址:https://www.cnblogs.com/imimjx/p/14389456.html
Copyright © 2011-2022 走看看