How to GROUP_CONCAT where there are null values in MYSQL will fields from LEFT JOIN?

这一生的挚爱 提交于 2019-12-11 18:05:52

问题


This is related to my previous example question submitted here. I have the following sql query :

SELECT 
listings.*,
region.REGION_ID,
#GROUP_CONCAT(region.REGION_ID) AS GROUP_IDs,
......
......
INNER JOIN schedule ON schedule.SCHEDULE_ID = listings.LISTING_SCHEDULE_ID 
LEFT JOIN listing_region ON listing_region.LIST_REGION_LISTING_ID = 
listings.LISTING_ID 
......
......
WHERE listings.LISTING_ID IN 
(SELECT LISTING_ID FROM listings WHERE ..........

The result is as follows :

I get multiple RegionIDs for the same ListingID and rest is null (expected) for non-existing records from LEFT join.

Now I need to group region_Ids for the same Listing ID.

So the only change I did was commenting out "region.REGION_ID" and enabled "GROUP_CONCAT(region.REGION_ID) AS GROUP_IDs,".

The result is as follows :

I do not understand why null rows are now dropped. I want them...! What should I correct here?


回答1:


The only change I was supposed to do to Add: GROUP BY ListingID to main filter clause.



来源:https://stackoverflow.com/questions/55442980/how-to-group-concat-where-there-are-null-values-in-mysql-will-fields-from-left-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!