如何只选择一个id为同一个sql的字段

时间:2013-08-01 08:16:48

标签: sql sql-server

我有一张如下表格

bugID  | name  | description  | comment
----------------------------------------------------------------
1      | bug1  | first bug    | <p></p>
1      | bug1  | first bug    | <p>this is the first bug</p>
1      | bug1  | first bug    | <p>this needs fixing</p>
2      | bug2  | second bug   | <p>this is the second bug</p>
3      | bug3  | third bug    | <p>bug number 3</p>

如果我从这张表中选择*,我想按如下方式取回记录

1, bug1, first bug, <p></p>, <p>this is the first bug</p>, <p>this needs fixing</p>
2, bug2, second bug, <p>this is the second bug</p>
3, bug3, third bug, <p>bug number 3</p>

我有可能在SQL中执行此操作吗?

1 个答案:

答案 0 :(得分:1)

假设这是SQL Server

SELECT bugID,
       name,
       description,
       STUFF(( SELECT ',' + comment 
                  FROM Table1 I Where I.bugID= O.bugID
                FOR
                  XML PATH('')
                ), 1, 1, '')
FROM   Table1 O
GROUP BY bugID,
       name,
       description