拼接行政区划(有上级时显示上级),查询行政区划

 select
    __T1.AREA_CODE as ID,
    area1.AREA_NAME as provinceName,
    area2.AREA_NAME as cityName,
    area3.AREA_NAME as disinctName,
    case when area1.AREA_NAME = area2.AREA_NAME then area1.AREA_NAME  when area2.AREA_NAME = area3.AREA_NAME then area1.AREA_NAME + '/' + area2.AREA_NAME
    else area1.AREA_NAME + '/' + area2.AREA_NAME+ '/' + area3.AREA_NAME end as areaName 
        FROM
     ss_sys_area __T1   
    left join ss_sys_area area1 on  area1.AREA_CODE = left(__T1.AREA_CODE ,2) + '0000' and  area1.TENANT_ID = '1a8b15af-830f-4f30-8ae8-a43a7bb454f7' 
    left join ss_sys_area area2  on   area2.AREA_CODE = left(__T1.AREA_CODE ,4) + '00' AND area2.TENANT_ID = '1a8b15af-830f-4f30-8ae8-a43a7bb454f7' 
    left join ss_sys_area area3  on   area3.AREA_CODE = __T1.AREA_CODE AND area3.TENANT_ID = '1a8b15af-830f-4f30-8ae8-a43a7bb454f7' 
    where   __T1.TENANT_ID = '1a8b15af-830f-4f30-8ae8-a43a7bb454f7' 

 

 

 

 

查询时可以直接创建一个视图,然后left join这个视图,然后取出拼接后的名称v.areaName

 

CREATE VIEW V_SYSTEM_AREA_CUSTOM
 AS
  select
    __T1.AREA_CODE as ID,
    area1.AREA_NAME as provinceName,
    area2.AREA_NAME as cityName,
    area3.AREA_NAME as disinctName,
    case when area1.AREA_NAME = area2.AREA_NAME then area1.AREA_NAME  when area2.AREA_NAME = area3.AREA_NAME then area1.AREA_NAME + '/' + area2.AREA_NAME
    else area1.AREA_NAME + '/' + area2.AREA_NAME+ '/' + area3.AREA_NAME end as areaName 
        FROM
     ss_sys_area __T1   
    left join ss_sys_area area1 on  area1.AREA_CODE = left(__T1.AREA_CODE ,2) + '0000' and  area1.TENANT_ID = '1a8b15af-830f-4f30-8ae8-a43a7bb454f7' 
    left join ss_sys_area area2  on   area2.AREA_CODE = left(__T1.AREA_CODE ,4) + '00' AND area2.TENANT_ID = '1a8b15af-830f-4f30-8ae8-a43a7bb454f7' 
    left join ss_sys_area area3  on   area3.AREA_CODE = __T1.AREA_CODE AND area3.TENANT_ID = '1a8b15af-830f-4f30-8ae8-a43a7bb454f7' 
    where   __T1.TENANT_ID = '1a8b15af-830f-4f30-8ae8-a43a7bb454f7' 

 

 

2、查询省级,可以查询出下面的所有市县;查询市级,可以查询出下面的所有区县,where 1=1

and  TB_UNIT_CODE like

(case when left( #{TB_UNIT_CODE},2) + '0000' = #{TB_UNIT_CODE} then left(#{TB_UNIT_CODE} ,2) + '%'   

when left(#{TB_UNIT_CODE} ,4) + '00' = #{TB_UNIT_CODE} then left(#{TB_UNIT_CODE} ,4) +'%'

else  #{TB_UNIT_CODE} end )

 

posted on 2024-05-14 15:55  五官一体即忢  阅读(19)  评论(0编辑  收藏  举报

导航