SQL结果转换为多个数组

huangapple go评论56阅读模式
英文:

SQL Result to multiple array

问题

你可以使用 SQL 查询来实现所需的结果。你可以使用 GROUP BYGROUP_CONCAT 函数来将具有相同员工的行合并为所需的格式。以下是一个示例 SQL 查询:

SELECT staff, GROUP_CONCAT(province SEPARATOR ', ') AS province
FROM your_table_name
GROUP BY staff;

记得将 your_table_name 替换为你实际使用的表名。这个查询将按员工分组,并使用逗号和空格作为分隔符,将相同员工的省份合并成一个字段,并显示所需的结果格式。

英文:

MY SQL returns the following array...

id staff province
1 Ben Ontario
2 Ben Quebec
3 John Manitoba
4 John Saskatchewan
6 Kitty Alberta
7 Kitty Nova Scotia

I would like to have the record displayed like this...

staff province
Ben Ontario, Quebec
John Quebec, Manitoba, Saskatchewan
Kitty Alberta, Nova Scotia

what approach should I use to approach this?

答案1

得分: 1

以下是翻译好的部分:

"Would be better to post the tables as well for clearer context.
You can use Aggregate functions and Grouping to help doing this. A GROUP BY to group the rows by staff column, then use GROUP_CONCAT() to concatenate province values in one string.

A reference of how you want it to be, unsure what table you are using or if there are any other factors but you can adapt as needed.

SELECT staff, GROUP_CONCAT(province SEPARATOR ', ') as province
FROM table_name
GROUP BY staff;"

请注意,代码部分不需要翻译。

英文:

Would be better to post the tables as well for clearer context.

You can use Aggregate functions and Grouping to help doing this. A GROUP BY to group the rows by staff column, then use GROUP_CONCAT() to concatenate province values in one string.

A reference of how you want it to be, unsure what table you are using or if there are any other factors but you can adapt as needed.

SELECT staff, GROUP_CONCAT(province SEPARATOR ', ') as province
FROM table_name
GROUP BY staff;

huangapple
  • 本文由 发表于 2023年2月18日 18:40:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定