簡體   English   中英

如何從兩個表中查詢第三個條件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM