我有两张 table 。设施_帖子包含所有可用的设施和属性_设施仅包含属性设施。 我想运行一个联接查询,如果在properties_amenities 中找不到行,该查询将返回comfortunes_posts 中的所有行,并带有NULL。
当前查询:
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 的便利设施行。
最佳答案
如果您想显示设施_帖子中的所有行,则需要将其放在 LEFT JOIN 的左侧。
...
FROM amenities_posts
LEFT JOIN properties_amenities ...
左联接选择左侧的所有行,并且仅选择右侧的匹配行。
关于php - MYSQL LEFT JOIN 不显示空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48835510/