gpt4 book ai didi

mysql - 将一列中的数据分组到单个单元格中输出 mySQL 结果

转载 作者:可可西里 更新时间:2023-11-01 07:33:14 25 4
gpt4 key购买 nike

我有一个 mySQL 查询:

SELECT work.ID AS workID
,team.ID AS team_ID
,team.name AS teamName
FROM work
LEFT OUTER JOIN team_work ON team_work.work_ID = work.ID
LEFT OUTER JOIN team ON team_work.team_ID = team.ID

返回以下示例结果:

workID   team_ID   teamName
1 10 Support
2 20 Dev
2 10 Support
3 30 Admin
4 40 Research

我想以表格格式将其输出到屏幕,以便每个 workID 有一行,而列有团队名称列表。像这样的东西:

WORK ID  |   TEAM NAME
-------------------------

1 | Support

2 | Dev
| Support

3 | Admin

4 | Research

我知道一个查询中的一个查询不是正确的方式(尽管它是最简单的)。我在嵌套数组上看到过一些东西,但作为一个新手,我不太清楚如何去做。

有人想花点时间说服我离开壁架吗?

谢谢...

最佳答案

SELECT work.ID AS workID
,group_concat(teamName) AS teamName
FROM work
LEFT OUTER JOIN team_work ON team_work.work_ID = work.ID
LEFT OUTER JOIN team ON team_work.team_ID = team.ID
group by work.ID

关于mysql - 将一列中的数据分组到单个单元格中输出 mySQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12978434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com