SQL,如何连接结果? [英] SQL, How to Concatenate results?
问题描述
我目前有一个SQL查询,该查询返回许多字段。我需要一个字段才能有效地成为子查询子。
问题的详细信息:
如果我有一个包含两列的表X,分别是ModuleID和说ModuleValue,我该如何编写SQL查询以获取结果并将其连接到一个字段中:
EG 返回的结果是
(从Table_X WHERE ModuleID = @ ModuleID中选择ModuleValue)
值1
值2
值3
...
我需要返回结果(作为单行,与上面的不同):
值1,值2,值3
是否有一个简单的串联方法可以被用户使用?
编辑:
DB是MS TSQL(2005)
使用MSSQL,您可以执行以下操作:
declare @result varchar(500)
set @result =''
从TableX选择@result = @result + ModuleValue +','
其中ModuleId = @ModuleId
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.
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:
EG Results returned from
(SELECT ModuleValue FROM Table_X WHERE ModuleID=@ModuleID)
Value 1
Value 2
Value 3
...
I need to return the result thus (as a single row, unlike the above):
Value 1, Value 2, Value 3
Is there a simple Concatenation method that could be user?
EDIT:
DB is MS TSQL (2005)
With MSSQL you can do something like this:
declare @result varchar(500)
set @result = ''
select @result = @result + ModuleValue + ', '
from TableX where ModuleId = @ModuleId
这篇关于SQL,如何连接结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!