当前位置: 代码迷 >> Sql Server >> 省市区三表查询
  详细解决方案

省市区三表查询

热度:16   发布时间:2016-04-24 23:17:35.0
省市区3表查询
根据选择的省,来查询出市区
create table province(
id int identity(1,1) primary key,
pname varchar(20)
)
create table citys(
cid int identity(1,1) primary key,
cname varchar(20),
cpid int constraint cp_id references province(id) 
)
create table district(
did int identity(1,1) primary key,
dname varchar(20),
cdid int constraint cd_id references citys(cid) 
)

------解决方案--------------------
市查询
select * from citys where pid = 1
区查询
select * from district where cdid in (
select cid from citys where pid = 1
)
  相关解决方案