一个巧妙的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,如有更好的方法,可以推荐
原文地址:https://www.cnblogs.com/senjiang/p/9644745.html