请教:查询a表数据,显示省市区镇的名字,不显示id,并且分页,请问sql语句怎么写
发布网友
发布时间:2023-02-14 19:22
我来回答
共1个回答
热心网友
时间:2023-09-13 04:46
创建表及数据
create table t
(id int,
code int,
name varchar(20),
parentcode int)
insert into t values (1,10000,'浙江',0)
insert into t values (2,20000,'安徽',0)
insert into t values (3,11000,'杭州',10000)
insert into t values (4,12000,'宁波',10000)
insert into t values (5,21000,'合肥',20000)
insert into t values (6,22000,'宣城',20000)
insert into t values (7,11100,'萧山',11000)
insert into t values (8,11300,'滨江',11000)
insert into t values (9,11300,'富阳',11000)
执行
select t3.name1,t3.name2,t4.name from
(select t1.code code1,t1.name name1,t1.parentcode parentcode1,t2.code code2,t2.name name2,t2.parentcode parentcode2
from t t1 left join t t2 on t1.code=t2.parentcode where t1.parentcode=0) t3 left join t t4 on t3.code2=t4.parentcode
结果
以上sqlserver写法,其他数据库基本也差不多