简体   繁体   English

MYSQL LEFT JOIN不显示空行

[英]MYSQL LEFT JOIN not showing Null Rows

I have 2 tables. 我有2张桌子。 amenities_posts containing all available amenities and properties_amenities containing only a properties amenities. comforts_posts包含所有可用的comfortities和property_amenities,仅包含property设施。 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. 我想运行一个联接查询,如果在properties_amenities中未找到行,则将返回带有NULL的comforts_posts中的所有行。

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. 此查询仅返回来自properties_amenities行,而不是所有带有null的便利设施行。

If you want to show all rows from amenities_posts, it needs to go on the left side of the LEFT JOIN. 如果要显示commendations_posts中的所有行,则需要在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. 左联接从左侧选择所有行,而仅从右侧选择匹配的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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