简体   繁体   中英

MYSQL LEFT JOIN not showing Null Rows

I have 2 tables. amenities_posts containing all available amenities and properties_amenities containing only a properties amenities. I would like to run a join query that will return all rows from amenities_posts with NULL if a row is not found in properties_amenities.

Current Query:

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;

This query only returns rows from properties_amenities and not all amenities rows with nulls.

If you want to show all rows from amenities_posts, it needs to go on the left side of the LEFT JOIN.

...
FROM amenities_posts
LEFT JOIN properties_amenities ...

A left join selects all rows from the left side, and only matching rows from the right side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM