具有 2 个连接的 MySQL group_concat 返回不需要的结果
执行此查询时,我希望得到 2 个手机号码和 1 个类别,但我得到 2 个类别,我做错了什么? 我想这与我加入事物的方式有关?
用户可以有多个imei, categoryjoin 将用户链接到多个类别
SELECT
u.*,
group_concat(i.mobilenumber) as mobilenumbers,
group_concat(c.name) as categories
FROM
users AS u
INNER JOIN
categoryjoin AS cj
ON
u.uid = cj.user_id
INNER JOIN
categories AS c
ON
cj.category_id = c.uid
INNER JOIN
imei AS i
ON
u.uid = i.user_id
GROUP BY
u.uid
非常感谢您的帮助!
When executing this query i expect te get 2 mobilenumbers and 1 category, instead i get 2 categories, what am i doing wrong?
I guess it has to do with the way i am joining things?
User, can have multiple imei's,
categoryjoin links a user to multiple categories
SELECT
u.*,
group_concat(i.mobilenumber) as mobilenumbers,
group_concat(c.name) as categories
FROM
users AS u
INNER JOIN
categoryjoin AS cj
ON
u.uid = cj.user_id
INNER JOIN
categories AS c
ON
cj.category_id = c.uid
INNER JOIN
imei AS i
ON
u.uid = i.user_id
GROUP BY
u.uid
Big pre-thanks you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果用户匹配一个类别,但匹配 imei 中的 2 行,则该类别将在结果集中重复。您可以使用
DISTINCT
删除 group_concat 中的冗余值:If a user matches one category, but matches 2 rows in imei, then the category will be duplicated in the result set. You can get rid of redundant values from group_concat using
DISTINCT
: