鉴于模型
Photo
======
id
PhotoConnect
======
outside_name
outside_key
photo_id (foreign key to Photo)
我如何找到所有带有PhotoConnect
的{{1}} = outside_name
和country
= outside_key
}和({{ {1}} = 6
和outside_name
= region
)?
到目前为止,我已经尝试了内部加入表格和outside_key
两者,但没有成功。
答案 0 :(得分:1)
听起来你想第二次加入这个表......国家或地区的匹配条件
SELECT
p.id
FROM Photo p
JOIN PhotoConnect pc on pc.photo_id = p.id
JOIN PhotoConnect pc1 on pc1.photo_id = p.id
WHERE pc.outside_name = 'country' and pc.outside_key = 6
AND pc1.outside_name = 'region' and pc1.outside_key = 13
GROUP BY photo_id