我有三张表: 地点 用户位置 图片
我需要进行一个查询,将用户喜欢的所有位置与第三个表格图像中的位置图像结合在一起。
现在我设法做到了这一点:
"SELECT Location.* FROM Location LEFT JOIN UserLocation ON Location.id = UserLocation.location_id WHERE UserLocation.user_id=:user_id:
但它只检索用户喜欢的位置。现在我需要每个位置的图像。我该怎么做?
最佳答案
您需要将条件移至 on
子句:
SELECT l.*, (ul.user_id is not null) as hasUser
FROM Location l LEFT JOIN
UserLocation ul
ON l.id = ul.location_id and
ul.user_id = :user_id:
where
子句过滤输出,将 LEFT JOIN
转换为 INNER JOIN
。
关于php - 如何从两个表中查询第三个表的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23059125/