Php 如何在第三个表的条件下从两个表进行查询
我有三张桌子: 位置 用户位置 形象 我需要做一个查询,将用户喜欢的所有位置与第三个表图像中的位置图像结合起来 目前,我设法做到了这一点:Php 如何在第三个表的条件下从两个表进行查询,php,mysql,sql,Php,Mysql,Sql,我有三张桌子: 位置 用户位置 形象 我需要做一个查询,将用户喜欢的所有位置与第三个表图像中的位置图像结合起来 目前,我设法做到了这一点: "SELECT Location.* FROM Location LEFT JOIN UserLocation ON Location.id = UserLocation.location_id WHERE UserLocation.user_id=:user_id: 但它只检索用户喜欢的位置。现在我需要每个位置的图像。如何操作?您需要将条件移动到on子句
"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
子句过滤输出,将左连接
转换为内部连接
谢谢您的回答。但我认为你不明白。我还需要图像表中每个位置的图像这是我不能做的。我可以拍摄用户喜欢的位置,但不能在同一查询中从第三个表中拍摄图像