选择多行,然后为每一行加入

时间:2015-06-25 09:56:10

标签: mysql

我有一个表格,用于存储location user正在观看的每个usercity。该表名为SELECT,如下图所示。

enter image description here

我需要user_id='1'所有行WHERE location_id,然后从location表中检索每个 SELECT b.* FROM usercity a JOIN location b ON a.location_id=b.id WHERE user_id='1' 的记录,如下图所示:

enter image description here

我不确定从哪里开始,但这是我的蹩脚尝试:

{{1}}

编辑我的查询使用一条记录。不确定它是否适用于多行。

3 个答案:

答案 0 :(得分:1)

try below-

select a.*, b.* 
from usercity a JOIN location b ON a.location_id=b.id
WHERE user_id=:uid 

答案 1 :(得分:1)

If its one-to-one relation then you can use inner join

select
uc.*,
l.* from usercity uc
join location l on l.location_id = uc.location_id
where l.user_id=:uid

答案 2 :(得分:0)

well, i kept playing with it and got what i want:

SELECT b.* FROM usercity a JOIN location b ON a.location_id=b.id WHERE user_id='1'

I guess SQL is one of those things you learn through practice.