Heim > Artikel > Backend-Entwicklung > mysql一行对应多列的关联查询有关问题,有实例及详细描述
mysql一行对应多列的关联查询问题,有实例及详细描述,求助
有两个表
t1表有 4个字段 id,city_id1,city_id2,city_id3
t2表有2个字段 id,city_name
t1表中的city_id1,2,3都是和t2表中的id对应
例如:
t1表有一条记录 1,26,29,50
t2表有三条记录
26 北京
29 上海
50 天津
现在我的需求是 希望能一条sql语句查出来t1中这条记录,并显示city_id1,2,3对应的城市 例如
结果: 1,北京,上海,天津
sql语句应该怎么写?
------解决思路----------------------
select id,<br /> (select city_name from t2 where id=t1.city_id1) as city_name1,<br /> (select city_name from t2 where id=t1.city_id2) as city_name2,<br /> (select city_name from t2 where id=t1.city_id3) as city_name3<br /> from t1<br />