我有两个表:用户和城市 用户表的字段:
user_id, user_birth_city, user_current_city, user_school_city
用户表的字段:
city_id, city_name
如何显示用户的全部三个城市名称?
答案 0 :(得分:0)
Select b.city_id, b.city_name from user_table a, city_table b where
(a.user_birth_city=b.city_name OR a.user_current_city=b.city_name OR
a.user_school_city=b.city_name) AND user_id='(whatever your user_id is)';
SQL人士会评论说,由于from中的逗号,此查询将不起作用,但是我向您保证,它将成功。
答案 1 :(得分:0)
在表LEFT JOIN
上使用city
3次即可做到
SELECT u.user_id,c1.city_name,c2.city_name,c3.city_name
FROM user u
LEFT JOIN city c1 ON u.user_birth_city=c1.city_id
LEFT JOIN city c2 ON u.user_current_city=c2.city_id
LEFT JOIN city c3 ON u.user_school_city=c3.city_id
注释:user
是MySQL
中的保留表,您的表名不应该是