gpt4 book ai didi

mysql - 将列值组合在一个单元格中

转载 作者:行者123 更新时间:2023-11-29 00:33:17 25 4
gpt4 key购买 nike

我需要编写一个 SQL select 语句,将一列中的值组合到一个单元格中。

例如

table name: Customer_Hobbies
+------------+------------+-----------+
| CustomerId | Age | Hobby |
+------------+------------+-----------+
| 123 | 17 | Golf |
| 123 | 17 | Football |
| 324 | 14 | Rugby |
| 627 | 28 | Football |
+------------+------------+-----------+

应该返回...

+------------+------------+----------------+
| CustomerId | Age | Hobbies |
+------------+------------+----------------+
| 123 | 17 | Golf,Football |
| 324 | 14 | Rugby |
| 627 | 28 | Football |
+------------+------------+----------------+

这可能吗?

注意我知道数据的布局不是特别合理,但我无法更改。

最佳答案

你想要group_concat():

select customerId, age, group_concat(hobby) as hobbies
from t
group by customerId, age

关于mysql - 将列值组合在一个单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15571207/

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