SQL Server查询在一列中与另一列相关的多个值

时间:2010-11-29 18:48:56

标签: sql sql-server-2005

情况:此表包含Documents表和Users表之间的关系信息。某些用户需要审阅或批准文档(类型)。如果需要的话,我想把它放在我可以让所有评论者在一条线上的地方。因此,如果三个用户查看文档1,那么一行将有346,394,519作为值,因为这些是审阅者

表: xDocumentsUsers

DocID..UserID....Type...
1........386......approver
1........346......reviewer
1........394......reviewer..
1........519......reviewer..
4........408......reviewer..
5........408......reviewer..
6........408......reviewer..
7........386......approver..
7........111......readdone..
7........346......reviewer..
8........386......approver..
8........346......reviewer..
9........386......approver..
9........346......reviewer..
10.......386......approver..
11.......386......approver..
11......346......reviewer..
12......386......approver..
12......346......reviewer..
13......386......approver..
13......346......reviewer..
14......386......approver..
14......346......reviewer..
15......386......approver

所以期望的结果将是......

DocID..UserID ................类型...

1........386....................approver
1........346,394,519......reviewer.
4........408....................reviewer..
5........408....................reviewer..
6........408....................reviewer..
7........386....................approver..
7........111....................readdone..
7........346....................reviewer..
8........386....................approver..
8........346....................reviewer..
9........386....................approver..
9........346....................reviewer..
10......386....................approver..
11......386....................approver..
11......346....................reviewer..
12......386....................approver..
12......346....................reviewer..
13......386....................approver..
13......346....................reviewer..
14......386....................approver..
14......346....................reviewer..
15......386....................approver

3 个答案:

答案 0 :(得分:2)

FOR XML PATH是一个很好的解决方案。但是,您需要注意,它会将内部SELECTs结果集中的任何特殊字符转换为它们的xml等效字符 - 即&将在XML结果集中变为&。您可以使用内部结果集周围的REPLACE函数轻松恢复为原始字符。借用旁观者的前一个例子,它看起来像(注意SELECT作为REPLACE函数的第一个参数包含在()中:

--Concat
SELECT  t.ID,
    REPLACE((SELECT  tIn.Val + ','
        FROM    @Table tIn
        WHERE   tIn.ID = t.ID
        FOR XML PATH('')), '&', '&'))
FROM    @Table t
GROUP BY t.ID

答案 1 :(得分:1)

看看

Emulating MySQL’s GROUP_CONCAT() Function in SQL Server 2005

Is there a way to create a SQL Server function to “join” multiple rows from a subquery into a single delimited field?

一个简单的例子是

DECLARE @Table TABLE(
        ID INT,
        Val VARCHAR(50)
)
INSERT INTO @Table (ID,Val) SELECT 1, 'A'
INSERT INTO @Table (ID,Val) SELECT 1, 'B'
INSERT INTO @Table (ID,Val) SELECT 1, 'C'
INSERT INTO @Table (ID,Val) SELECT 2, 'B'
INSERT INTO @Table (ID,Val) SELECT 2, 'C'

--Concat
SELECT  t.ID,
        (
            SELECT  tIn.Val + ','
            FROM    @Table tIn
            WHERE   tIn.ID = t.ID
            FOR XML PATH('')
        )
FROM    @Table t
GROUP BY t.ID

答案 2 :(得分:0)

这有帮助吗?

SELECT DocID
     , [Type]
     , (SELECT CAST(UserID + ', ' AS VARCHAR(MAX)) 
         FROM [xDocumentsUsers] 
         WHERE (UserID = x1.UserID) 
         FOR XML PATH ('')
      ) AS [UserIDs]
FROM [xDocumentsUsers] AS x1