GROUP_CONCAT and LEFT_JOIN issue - Not all rows returned

*爱你&永不变心* 提交于 2019-12-06 11:51:19
altermativ

Ah, found my answer:

You should never forget the GROUP BY clause when working with GROUP_CONCAT. I was missing GROUP BY T_PRODUCT.id_product in my second query.

I'll leave the question up in case someone is as absent-minded as I am.

Edit:

From this answer, I figured I can also activate the SQL Mode ONLY_FULL_GROUP_BY to force MySQL to throw an error in case the GROUP BY is missing or incorrect.

Alternately, you could use a subquery, just test for performance

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