查詢省市區,轉成 json 表

2021-07-10 22:03:23 字數 2355 閱讀 2191

sql語句主要用到以下幾點:

1. group_concat   用來將分組之後的專案拼接在一起,用逗號隔開。

3. 多個 select 巢狀。 

4. 查詢結果欄位起別名...

sql語句

select ('') as last from (select group_concat(items) as itemtog from (select ('"'||province||'":') as items from (select group_concat(cityareas) as cityoutput,province    from (select ('"'||bbb.cityname||'":'||bbb.aa) as cityareas,cityid,father from (select aaa.aa as aa,hat_city.cityid as cityid , hat_city.city as cityname ,hat_city.father as father  from (select ("[" || a.allarea || "]") as aa , cityid from (select father as cityid,group_concat(area) as allarea from (select hat_area.father as father, ('"'||hat_area.area||'"') as area from hat_area)  group by father) as a) as aaa , hat_city where aaa.cityid = hat_city.cityid) as bbb) as bkbk, hat_province where bkbk.father = hat_province.provinceid group by provinceid) as citysout) as output)

create table hat_area (

id int(11) not null auto_increment,

areaid varchar(6) default null,

area varchar(60) default null,

father varchar(6) default null,

primary key(id)

) engine=myisam auto_increment=3145 default charset=utf8;

create table hat_city (

id int(11) not null auto_increment,

cityid varchar(6) default null,

city varchar(50) default null,

father varchar(6) default null,

primary key (id)

) engine=myisam auto_increment=346 default charset=utf8;

create table hat_province (

id int(11) not null auto_increment,

provinceid varchar(6) default null,

province varchar(40) default null,

primary key (id)

) engine=myisam auto_increment=35 default charset=utf8;

執行sql的查詢結果

,

"天津市": ,

"河北省": ,

"山西省": ,

"內蒙古自治區": ,

"遼寧省": ,

"吉林省": ,

"黑龍江省": ,

"上海市": ,

"江蘇省": ,

"浙江省": ,

"安徽省": ,

"福建省": ,

"江西省": ,

"山東省": ,

"河南省": ,

"湖北省": ,

"湖南省": ,

"廣東省": ,

"廣西壯族自治區": ,

"海南省": ,

"重慶市": ,

"四川省": ,

"貴州省": ,

"雲南省": ,

"**自治區": ,

"陝西省": ,

"甘肅省": ,

"青海省": ,

"寧夏回族自治區": ,

"新疆維吾爾自治區":

}

省市區表結構

create table gj area id varchar 50 not null default comment id name varchar 50 not null default comment 名稱 display name varchar 50 not null default co...

全國省市區資料SQL 省市區

drop table if exists provinces create table provinces id int 11 not null auto increment,provinceid varchar 20 not null,province varchar 50 not null,pr...

省市區陣列

先找到檔案對應的路徑 nsstring path users dlios desktop oc5 省市區陣列 oc5 省市區陣列 area.txt 通過路徑產生字串,內容就是txt文字裡的內容 nsstring str nsstring stringwithcontentsoffile path e...