Mysql Group_Concat函数

发布于 2024-10-12 11:10:16 字数 993 浏览 9 评论 0原文

不理解如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-10-19 11:10:16
select group_concat(distinct co.name)

详细信息: http://dev.mysql .com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

语法

GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
select group_concat(distinct co.name)

details : http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

syntax

GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
坐在坟头思考人生 2024-10-19 11:10:16
SELECT n.cid, n.Title,
  GROUP_CONCAT(DISTINCT co.name ORDER BY co.sort) as organizers, 
  GROUP_CONCAT(cp.name ORDER BY cp.sort) 
FROM Content n 
LEFT JOIN Organizers co ON n.cid = co.cid 
LEFT JOIN Participants cp ON n.cid = cp.cid 
GROUP BY n.cid
SELECT n.cid, n.Title,
  GROUP_CONCAT(DISTINCT co.name ORDER BY co.sort) as organizers, 
  GROUP_CONCAT(cp.name ORDER BY cp.sort) 
FROM Content n 
LEFT JOIN Organizers co ON n.cid = co.cid 
LEFT JOIN Participants cp ON n.cid = cp.cid 
GROUP BY n.cid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文