比如: 地区表: ID AreaName ----------------------- 1 四川省 2 广东省 3 成都市 4 广州市 用户表: ID UserName ProvinceID CityID ----------------------------------------- 1 test 1 3 2 test1 2 4查询出用户的省市名称
select * from 用户表 a,地区表 b where a.CityID =b.ID
select UserName, Province=P.AreaName, City=C.AreaName from 用户表 U join 地区表 P on U.ProvinceID=P.ID join 地区表 C on U.CityID=C.ID where UserName='test'
select a.AreaName ,b.AreaName from 用户表 u inner join 地区表 a on u.ProvinceID=a.ID inner join 地区表 b on u.ProvinceID=b.ID
--> By dobear_0922(小熊) 2008-12-26 10:56:50 --> 测试数据:[地区表] if object_id('[地区表]') is not null drop table [地区表] create table [地区表]([ID] int,[AreaName] varchar(6)) insert [地区表] select 1,'四川省' union all select 2,'广东省' union all select 3,'成都市' union all select 4,'广州市' --> 测试数据:[用户表] if object_id('[用户表]') is not null drop table [用户表] create table [用户表]([ID] int,[UserName] varchar(5),[ProvinceID] int,[CityID] int) insert [用户表] select 1,'test',1,3 union all select 2,'test1',2,4select UserName, Province=P.AreaName, City=C.AreaName from 用户表 U join 地区表 P on U.ProvinceID=P.ID join 地区表 C on U.CityID=C.ID --where UserName='test' /* UserName Province City -------- -------- ------ test 四川省 成都市 test1 广东省 广州市(2 行受影响) */drop table 用户表,地区表
select ID, UserName, 省 = (select areaname from 地区表 b where id = a.ProvinceID), 市 = (select areaname from 地区表 b where id = a.CityID) from 用户表 a
create table [地区表]([ID] int,[AreaName] varchar(6)) insert [地区表] select 1,'四川省' union all select 2,'广东省' union all select 3,'成都市' union all select 4,'广州市' --> 测试数据:[用户表] if object_id('[用户表]') is not null drop table [用户表] create table [用户表]([ID] int,[UserName] varchar(5),[ProvinceID] int,[CityID] int) insert [用户表] select 1,'test',1,3 union all select 2,'test1',2,4select ID, UserName, 省 = (select areaname from 地区表 b where id = a.ProvinceID), 市 = (select areaname from 地区表 b where id = a.CityID) from 用户表 a drop table 地区表,用户表/* ID UserName 省 市 ----------- -------- ------ ------ 1 test 四川省 成都市 2 test1 广东省 广州市(所影响的行数为 2 行) */
地区表:
ID AreaName
-----------------------
1 四川省
2 广东省
3 成都市
4 广州市
用户表:
ID UserName ProvinceID CityID
-----------------------------------------
1 test 1 3
2 test1 2 4查询出用户的省市名称
select UserName, Province=P.AreaName, City=C.AreaName
from 用户表 U join 地区表 P on U.ProvinceID=P.ID
join 地区表 C on U.CityID=C.ID
where UserName='test'
inner join 地区表 b on u.ProvinceID=b.ID
--> 测试数据:[地区表]
if object_id('[地区表]') is not null drop table [地区表]
create table [地区表]([ID] int,[AreaName] varchar(6))
insert [地区表]
select 1,'四川省' union all
select 2,'广东省' union all
select 3,'成都市' union all
select 4,'广州市'
--> 测试数据:[用户表]
if object_id('[用户表]') is not null drop table [用户表]
create table [用户表]([ID] int,[UserName] varchar(5),[ProvinceID] int,[CityID] int)
insert [用户表]
select 1,'test',1,3 union all
select 2,'test1',2,4select UserName, Province=P.AreaName, City=C.AreaName
from 用户表 U join 地区表 P on U.ProvinceID=P.ID
join 地区表 C on U.CityID=C.ID
--where UserName='test'
/*
UserName Province City
-------- -------- ------
test 四川省 成都市
test1 广东省 广州市(2 行受影响)
*/drop table 用户表,地区表
UserName,
省 = (select areaname from 地区表 b where id = a.ProvinceID),
市 = (select areaname from 地区表 b where id = a.CityID)
from 用户表 a
insert [地区表]
select 1,'四川省' union all
select 2,'广东省' union all
select 3,'成都市' union all
select 4,'广州市'
--> 测试数据:[用户表]
if object_id('[用户表]') is not null drop table [用户表]
create table [用户表]([ID] int,[UserName] varchar(5),[ProvinceID] int,[CityID] int)
insert [用户表]
select 1,'test',1,3 union all
select 2,'test1',2,4select ID,
UserName,
省 = (select areaname from 地区表 b where id = a.ProvinceID),
市 = (select areaname from 地区表 b where id = a.CityID)
from 用户表 a
drop table 地区表,用户表/*
ID UserName 省 市
----------- -------- ------ ------
1 test 四川省 成都市
2 test1 广东省 广州市(所影响的行数为 2 行)
*/