[英]How to query from two tables with the condition of a third
我有三個表:location user_location圖片
我需要進行一個查詢,將用戶喜歡的所有位置信息與第三張表圖像中的位置信息結合在一起。
現在我設法做到這一點:
"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
的方式過濾輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.