还有一种写法叫Hierarchical,使用递归方法。具体参见https://en.wikipedia.org/wiki/Hierarchical_and_recursive_queries_in_SQL
create table State--国家 ( StateID int primary key not null,--国家id StateName varchar(20)not null,--国家名称 ) insert into Provinces values(1,'中国') insert into Provinces values(2,'美国') insert into Provinces values(3,'英国') create table Provinces--省份表 ( ProvincesID int primary key not null,--省ID StateID int foreign key references State(StateID),--对应国家ID ProvincesName varchar(20)not null,--省份名称 ) insert into Provinces values(1,1,'广东省') insert into Provinces values(1,2,'湖南省') insert into Provinces values(1,3,'河南省') insert into Provinces values(1,4,'海南省') insert into Provinces values(1,5,'福建省') create table City--城市表 ( CityID int primary key not null,--城市ID CityName varchar(20)not null,--城市名字 StateID int Not null,--对应国家ID ProvincesID int foreign key references Provinces(ProvincesID),--对应省ID ) insert into City values(1,1,1,'广州市') insert into City values(1,1,2,'茂名市') insert into City values(1,1,3,'深圳市') insert into City values(1,1,4,'东莞市') insert into City values(1,1,5,'珠海市') insert into City values(1,2,1,'长沙市') insert into City values(1,2,2,'永州市') insert into City values(1,2,3,'湘潭市') insert into City values(1,2,4,'衡阳市') insert into City values(1,3,1,'郑州市') insert into City values(1,3,2,'洛阳市') insert into City values(1,3,3,'平顶山市') insert into City values(1,4,1,'海口市') insert into City values(1,4,2,'三亚市') insert into City values(1,4,3,'琼海市') insert into City values(1,5,1,'福州市') insert into City values(1,5,2,'厦门市') 你想要的三级联动,国家-省-城市欢迎分享,转载请注明来源:内存溢出
评论列表(0条)