题目:一张表设计出如下所示的结构,并查询出所有街道
广东
--珠海
--片区1
--街道1
--街道2
--片区2
--街道3
解1:
create table `zhuhai1`(
id smallint unsigned primary key auto_increment,
pid smallint unsigned,
area_name varchar(50)
);
insert into `zhuhai1`(id,pid,area_name) values
(1,0,'广东'),
(2,1,'珠海'),
(3,2,'片区1'),
(4,2,'片区2'),
(5,3,'街道1'),
(6,3,'街道2'),
(7,4,'街道2');
select * from zhuhai1 where pid in (3,4);
解2:
create table `zhuhai`(
id smallint unsigned primary key auto_increment,
pid smallint unsigned,
area_name varchar(50),
area_type smallint comment '地区类型,1:省,2:市,3:片区,4:街道'
);
insert into `zhuhai`(id,pid,area_name,area_type) values
(1,0,'广东',1),
(2,1,'珠海',2),
(3,2,'片区1',3),
(4,2,'片区2',3),
(5,3,'街道1',4),
(6,3,'街道2',4),
(7,4,'街道2',4);
select * from zhuhai where area_type=4;