假如两个表 一个是用户表一个是城市表
City表(所有的省市区都存在一个表里面 通过upid来区分省市区)
id name upid
1 四川省 0
2 成都市 1
3 金牛区 2
然而用户表user中的字段里有
uid name province city district
1 测试 1 2 3
用户表中province city district对应的就是City表的id序号
那么我查询用户表的时候如何输出
1 测试 四川省 成都市 金牛区
你的upid,province,city,district字段都是int型的吧,如果upid是按0表示省,1表示市,2表示区的话
select uid,name,
(select name from city c where c.id = u.province and c.upid = 0),
(select name from city c where c.id = u.city and c.upid = 1),
(select name from city c where c.id = u.district and c.upid = 2)
from user u
Select user.name,
(Select City.name as province from City where id=user.province),
(Select City.name as city from City where id=user.city),
(Select City.name as district from City where id=user.district)
From user
通过两张表的关联字段查,要不给张中间表,要不给个关联字段