Sql select查询帮助
我有一个包含数据的数据库表,如下所示:
Primary key | Column1 | Column2 | col3 | col4 |col5
---------------------------------------------------------------------
1 | Chicago Bulls | Michael Jordan | 6'6 | aaaa | cccc
2 | Chicago Bulls | Scottie Pippen | 6'8 | zzzz | 345
3 | Utah Jazz | Malone | 6'9 | vvvv | xcxc
4 | Orlando Magic | Hardaway | 6'7 | xnnn | sdsd
我想编写一个查询,它将获取 Column1 中的所有不同值,并为每个 Column1 值附加 Column2 中的值。 例如:查询应该返回
**Chicago Bulls | Michael Jordan, Scottie Pippen**
**Utah Jazz | Malone**
**Orlando Magic | Hardaway**
我可以编写一个查询来获取第 1 列中的所有不同值,然后迭代每个不同值以在进行一些操作后获取附加的第 2 列。是否可以仅通过一个查询来完成整个工作?请帮助提供一个查询示例。 谢谢。
I have a database table with data as shown below:
Primary key | Column1 | Column2 | col3 | col4 |col5
---------------------------------------------------------------------
1 | Chicago Bulls | Michael Jordan | 6'6 | aaaa | cccc
2 | Chicago Bulls | Scottie Pippen | 6'8 | zzzz | 345
3 | Utah Jazz | Malone | 6'9 | vvvv | xcxc
4 | Orlando Magic | Hardaway | 6'7 | xnnn | sdsd
I want to write a query which will fetch all distinct values in Column1 and append values in Column2 for each Column1 value.
For eg: The query should return
**Chicago Bulls | Michael Jordan, Scottie Pippen**
**Utah Jazz | Malone**
**Orlando Magic | Hardaway**
I can write a query to fetch all distinct values in column1, then iterate over each distinct value to get the appended column 2 after some manipulation. Is it possible to do the entire job in only one query? Please help with an example of a query.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 MySQL
If you are using MySQL
如果您使用的是 SQL Server:
不知道为什么 Microsoft 使此操作如此困难,但据我所知,这是在 SQL Server 中执行此操作的唯一方法...
顺便说一句,您可能会考虑将 Column1 更改为查找表否则下次犹他州搬家的时候你就会讨厌生活了;)
If you are using SQL Server:
Not sure why Microsoft makes this one so hard, but as far as I know this is the only method to do this in SQL Server...
On a side note you might consider changing Column1 to a lookup table or the next time Utah moves you're going to be hating life ;)
SQL Anywhere 具有 list() 聚合功能正是为了这个目的。
如果您使用的是 SQL Server,那么除了在某种程度上滥用 XML 的棘手解决方案之外,您还可以使用任何 .net 语言编写自己的聚合函数。以及此功能的 Microsoft 文档完全使用字符串连接的情况作为示例。
SQL Anywhere has a list() aggregation function since more than ten years for exactly this purpose.
If you are using SQL Server, then apart from the tricky solution that abuses XML to some extent, you can write your own aggregation function in any .net language. And the Microsoft documentation of this feature uses exactly the case of string concatenation as an example.
此类问题的一般解决方案是聚合函数 ARRAY_AGG(),它返回一个包含不同行中的值的数组,可以选择按某些条件排序。该函数已被提议用于下一版本的 SQL 标准。 GROUP_CONCAT() 函数是一种特殊情况,它将数组转换为逗号分隔的字符串。
最新的 HSQLDB 2.0.1 支持 ARRAY_AGG() 和 GROUP_CONCAT()。
http://hsqldb.org/doc/2.0/guide/dataaccess- chapt.html#N12312
The general solution to this type of problem is the aggregate function ARRAY_AGG() that returns an array containing the values in different rows, optionally ordered by some criteria. This function has been proposed for the next version of the SQL Standard. The GROUP_CONCAT() function is a special case that converts the array to a comma separated string.
Both ARRAY_AGG() and GROUP_CONCAT() are supported by the latest HSQLDB 2.0.1.
http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#N12312