SQL,如何连接结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1339788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:20:57  来源:igfitidea点击:

SQL, How to Concatenate results?

sqlsql-serversql-server-2005concatenationsql-server-group-concat

提问by Darknight

I currently have a SQL query that returns a number of fields. I need one f the fields to be effectively a sub query sub that.

我目前有一个返回多个字段的 SQL 查询。我需要一个字段来有效地成为一个子查询子。

The Problem in detail:

问题详细:

If I have a table X with two columns, ModuleID and say ModuleValue, how can I write a SQL query to take the results and Concatenate it into one field:

如果我有一个包含两列 ModuleID 和 ModuleValue 的表 X,我如何编写 SQL 查询来获取结果并将其连接到一个字段中:

EGResults returned from

EG结果从

 (SELECT ModuleValue FROM Table_X WHERE ModuleID=@ModuleID)

Value 1

值 1

Value 2

值 2

Value 3

值 3

...

...

I need to return the result thus (as a single row, unlike the above):

我需要因此返回结果(作为单行,与上述不同):

Value 1, Value 2, Value 3

值 1、值 2、值 3

Is there a simple Concatenation method that could be user?

是否有一个简单的连接方法可以成为用户?

EDIT:

编辑:

DB is MS TSQL (2005)

DB 是 MS TSQL (2005)

采纳答案by Blorgbeard is out

With MSSQL you can do something like this:

使用 MSSQL,您可以执行以下操作:

declare @result varchar(500)
set @result = ''
select @result = @result + ModuleValue + ', ' 
from TableX where ModuleId = @ModuleId

回答by LukeH

This one automatically excludes the trailing comma, unlike most of the other answers.

与大多数其他答案不同,此答案会自动排除尾随逗号。

DECLARE @csv VARCHAR(1000)

SELECT @csv = COALESCE(@csv + ',', '') + ModuleValue
FROM Table_X
WHERE ModuleID = @ModuleID

(If the ModuleValuecolumn isn't already a string type then you might need to cast it to a VARCHAR.)

(如果该ModuleValue列还不是字符串类型,则您可能需要将其强制转换为VARCHAR.)

回答by Silvan Mühlemann

In mysql you'd use the following function:

在 mysql 中,您将使用以下函数:

SELECT GROUP_CONCAT(ModuleValue, ",") FROM Table_X WHERE ModuleID=@ModuleID

I am not sure which dialect you are using.

我不确定您使用的是哪种方言。

回答by marc_s

In SQL Server 2005 and up, you could do something like this:

在 SQL Server 2005 及更高版本中,您可以执行以下操作:

SELECT 
    (SELECT ModuleValue + ','
     FROM dbo.Modules
     FOR XML PATH('')
    ) 
FROM dbo.Modules
WHERE ModuleID = 1

This should give you something like what you're looking for.

这应该给你一些你正在寻找的东西。

Marc

马克

回答by sdsc81

In my opinion, if you are using SQL Server 2017 or later, using STRING_AGG( ... )is the best solution:

在我看来,如果您使用的是 SQL Server 2017 或更高版本,使用STRING_AGG( ... )是最好的解决方案:

More at:

更多在:

https://stackoverflow.com/a/42778050/1260488

https://stackoverflow.com/a/42778050/1260488

回答by idrosid

It depends on the database you are using. MySQL for example supports the (non-standard) group_concatfunction. So you could write:

这取决于您使用的数据库。例如,MySQL 支持(非标准)group_concat函数。所以你可以写:

SELECT GROUP_CONCAT(ModuleValue) FROM Table_X WHERE ModuleID=@ModuleID

Group-concat is not available at all database servers though.

但是,并非所有数据库服务器都提供 Group-concat。

回答by sandeep rawat

Small update on Marc we will have additional " , " at the end. i used stuff function to remove extra semicolon .

关于 Marc 的小更新,我们将在末尾添加额外的“,”。我使用了东西函数来删除多余的分号。

       SELECT STUFF((  SELECT ',' + ModuleValue AS ModuleValue
                           FROM ModuleValue WHERE ModuleID=@ModuleID
                      FOR XML PATH('') 
                     ), 1, 1, '' )