• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

三木筒子

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

一个巧妙的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,如有更好的方法,可以推荐

posted on 2018-09-14 09:48  森酱  阅读(107)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3