Mysql Group_Concat函数
不理解如何使用 GROUP_CONCAT 函数,因为我的查询似乎重复了我实际上并不想要的值。
我有三个表:
------------------
Content
------------------
cid | title
2334 | Lorem Ipsum
------------------
Organizers
------------------
cid | name | sort
2334 | John Doe | 0
------------------
Participants
------------------
cid | name | sort
2334 | Jane Doe | 0
2334 | Bob Hope | 1
2334 | Bart Simpson | 2
我正在尝试编写一个将生成的查询:
cid | title | organizers | participants
2334 | Lorem Ipsum | John Doe | Jane Doe, Bob Hope, Bart Simpson
因此,我正在尝试使用 GROUP_CONCAT。但是,值正在重复,所以我不确定如何输出我正在寻找的结果?
SELECT GROUP_CONCAT(co.name) as organizers,
GROUP_CONCAT(cp.name) FROM content n
LEFT JOIN organizers co ON n.cid = co.cid
LEFT JOIN participants cp ON n.cid = cp.cid
WHERE n.cid = 2334
上面的结果是:
cid | title | organizers | participants
2334 | Lorem Ipsum | John Doe, John Doe, John Doe | Jane Doe, Bob Hope, Bart Simpson
Not understanding how to use the GROUP_CONCAT function as my queries seem to repeat values that I don't actually want.
I have three tables:
------------------
Content
------------------
cid | title
2334 | Lorem Ipsum
------------------
Organizers
------------------
cid | name | sort
2334 | John Doe | 0
------------------
Participants
------------------
cid | name | sort
2334 | Jane Doe | 0
2334 | Bob Hope | 1
2334 | Bart Simpson | 2
I'm trying to write a query that will produce:
cid | title | organizers | participants
2334 | Lorem Ipsum | John Doe | Jane Doe, Bob Hope, Bart Simpson
So, I'm trying to use GROUP_CONCAT. But, values are being repeated, so I'm not sure how to output the results I'm looking for?
SELECT GROUP_CONCAT(co.name) as organizers,
GROUP_CONCAT(cp.name) FROM content n
LEFT JOIN organizers co ON n.cid = co.cid
LEFT JOIN participants cp ON n.cid = cp.cid
WHERE n.cid = 2334
The above yields:
cid | title | organizers | participants
2334 | Lorem Ipsum | John Doe, John Doe, John Doe | Jane Doe, Bob Hope, Bart Simpson
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
详细信息: http://dev.mysql .com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
语法
details : http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
syntax