sql字符串cat作为聚合函数:cat多条记录在一起

发布于 2024-11-07 15:04:32 字数 309 浏览 0 评论 0原文

我有一个包含列的表: (project_id, name)

这是一个人员列表,每个人都有其所属的项目。如果一个人在两个项目中,那就是重复的。

我想提取一个包含以下列的表: (project_id, people) 其中 people 是参与该项目的所有人员姓名的字符串 cat。猫必须用逗号分隔,如下所示:

12, john
12, mark
12, dave
14, luke

成为

12, "john, mark, dave"
14, "luke"

I have a table with columns: (project_id, name)

It's a list of people, each one with the project it belongs to. If a person is in two projects, it is duplicated.

I want to extract a table with columns: (project_id, people) where people is a string cat of the names of all person working on that project. The cat must be comma separated, like this:

12, john
12, mark
12, dave
14, luke

becomes

12, "john, mark, dave"
14, "luke"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

若水微香 2024-11-14 15:04:32

则可以通过简单的查询来完成此操作:

SELECT project_id, GROUP_CONCAT(name) as people
FROM table
GROUP BY project_id

如果您坚持在逗号后留有空格,

SELECT project_id, GROUP_CONCAT(name SEPARATOR ", ") as people
FROM table
GROUP BY project_id

you can do this with a simple query

SELECT project_id, GROUP_CONCAT(name) as people
FROM table
GROUP BY project_id

if you insist to have space after the comma:

SELECT project_id, GROUP_CONCAT(name SEPARATOR ", ") as people
FROM table
GROUP BY project_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文