执行此查询时,我希望获得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
非常感谢你的帮助!
答案 0 :(得分:2)
如果用户匹配一个类别,但匹配imei中的两行,则该类别将在结果集中重复。您可以使用DISTINCT
删除group_concat中的冗余值:
SELECT
u.*,
group_concat(distinct i.mobilenumber) as mobilenumbers,
group_concat(distinct c.name) as categories