省表a:ProvinceID,ProvinceName
市表b:cityid,cityname,ProvinceID
区表b:countryid,countryname,ProvinceID,cityid
怎么合并成如下格式?
比如:
山东省
山东省济南市
山东省济南市历下区
山东省济南市xxx区
山东省泰安市
山东省泰安市xxx区
湖南省
湖南省xxx市
我现在联合查询 就是三张表有规律的排下来,怎么写呢??
用 union all,把所有结果集拼接:
select distinct * from
(select '省'=ProvinceName,'市'=null,'区'=null from Province with(nolock)
union all
select '省'=p.ProvinceName,'市'=ct.cityName,'区'=null from Province p with(nolock) left join city ct on p.ProvinceID = ct.ProvinceID
union all
select '省'=p.ProvinceName,'市'=ct.cityName,'区'=c.countryname from Province p with(nolock) left join city ct on p.ProvinceID = ct.ProvinceID left join country c on c.cityid = ct.cityid) t
order by t.省 desc
select concat(ProvinceName,cityname,counttryname) AS address
from a join b.....
join c ....
这是要做三级联动下拉框吗?
select city1.name as '一级地名',
city2.name as '二级地名',
city3.name as '三级地名'
from city as city1 inner join city as city2 on city1.parentid = 0 and city1.id = city2.parentid
inner join city as city3 on city2.id = city3.parentid