create table dt_parent (
id int NOT NULL auto_increment comment '自增id',
pid int default -1 not null comment '子级标识',
name varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL comment '内容',
primary key (id)
)engine=innodb default charset=utf8mb3 comment='子父级测试表';
insert into dt_parent value
(1,0, '北京市'),
(2,1, '北京市'),
(3,2, '东城区'),
(4,2, '西城区'),
(5,0,'山西省'),
(6,5,'太原市'),
(7,5,'临时市'),
(8,7,'洪洞县'),
(9,7,'侯马市');
--查二级:
select a.name province,b.name city from dt_parent a inner join dt_parent b on b.pid=a.id where a.pid=0;
--查三级:
select c.name province,b.name city,a.name area from dt_parent a inner join dt_parent b on a.pid=b.id inner join dt_parent c on b.pid=c.id;
其他数据自己写,仅此说明三级结构问题。
更多请关注“智慧书生” 微信公众号。