[英]MYSQL LEFT JOIN not showing Null Rows
我有2張桌子。 comforts_posts包含所有可用的comfortities和property_amenities,僅包含property設施。 我想運行一個聯接查詢,如果在properties_amenities中未找到行,則將返回帶有NULL的comforts_posts中的所有行。
當前查詢:
SELECT properties_amenities.*, amenities_posts.type, amenities_posts.grouped, amenities_posts.title, properties_amenities.property_id
FROM properties_amenities
LEFT JOIN amenities_posts ON properties_amenities.amenity_id = amenities_posts.id
WHERE properties_amenities.property_id=318 OR properties_amenities.property_id IS NULL
ORDER BY amenities_posts.grouped DESC, amenities_posts.sortby ASC;
此查詢僅返回來自properties_amenities
行,而不是所有帶有null的便利設施行。
如果要顯示commendations_posts中的所有行,則需要在LEFT JOIN的左側 。
...
FROM amenities_posts
LEFT JOIN properties_amenities ...
左聯接從左側選擇所有行,而僅從右側選擇匹配的行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.