SQL查询 - 将多行合并为一行

时间:2016-04-19 21:11:08

标签: sql sql-server

如何编写SQL以将来自多个列和行的数据合并为一行。

OrganizationId  Name    Type    Active
--------------------------------------
      1         Baker   County  Yes
      2         curry   county  Yes

预期结果:

SomeColumnName
-------------------------------
1BakerCountyYes2currycountyYes

1 个答案:

答案 0 :(得分:0)

<强>的MySQL

使用 GROUP_CONCAT

SELECT row_id,        
       group_concat( Value ORDER BY Value SEPARATOR ' ')
FROM 
    (SELECT 
        @row_number:=1 AS row_id,
        CONCAT(OrganizationId ,Name,Type,Active) AS Value
    FROM
        Table1
     ORDER BY file_fields_id
     ) T
GROUP BY row_id;