zoukankan      html  css  js  c++  java
  • 一个巧妙的sql

    今天看项目时发现一个非常有意思的sql

    select distinct s.*,a.areaFullName
    from jh_shop s
    , jh_area a
    , jh_org o1
    , jh_org o2
    where 1=1
    and s.areaId = a.areaId
    and s.orgId = o2.orgId
    and o2.path like concat(o1.path,'%')
    and o1.orgId in
    <foreach collection="list" index="index" item="item" close=")" open="(" separator=",">
    #{item}
    </foreach>
    order by s.shopType ASC;

    这个虽然是看到的,但是这个确实写的非常精辟

    o1.path 是长成这样的   /1/2/4/33/54/123   可以看到,这个里不经使用了like,而且 使用了索引,使得整个效率得以了很好的提升,

    o2.path like concat(o1.path,'%') 这句话,查出了以o1为条件,o2为检索目标,从而检索出,整张表的父子关系,在结尾加上o1的范围,那么也就查出了想要的结果

    如有错误,请邮件zs253499660@sina.com,如有更好的方法,可以推荐
  • 相关阅读:
    P1891 疯狂LCM
    P2568 GCD
    P1516 青蛙的约会和P2421 [NOI2002]荒岛野人
    P4168 蒲公英
    P5960 差分约束算法模板
    P2024 食物链(种类并查集)
    CF1328E Tree Queries
    CF1328B K-th Beautiful String
    dij-spfa乱搞
    P1993 小K的农场
  • 原文地址:https://www.cnblogs.com/senjiang/p/9644745.html
Copyright © 2011-2022 走看看