如何在sql中的select中使用Stuff函数两次

时间:2017-07-17 12:37:41

标签: sql database sql-server-2012

如何在sql中的select中使用Stuff函数两次

表是:

+----+---------+-----------+
| Id | col_name|value      |
+----+---------+-----------+   
| 1  | name    |  mar      |
+----+---------+-----------+
| 1  | family  |  ah       |
+----+---------+-----------+ 
| 1  | phone   |  336      |
+----+---------+-----------+ 
| 1  | ID      |  1001     |
+----+---------+-----------+ 
| 1  | email   |  m.h@g.com|
+----+---------+-----------+ 
Required output:
+----+---------------------------+---------------------------+
| Id |     COL_NAME              |         Values            | 
+----+---------------------------+---------------------------+ 
|  1 | Name,Family,Phone,ID,Email|  mar,ah,336,1001,m.h@g.com|
+----+---------------------------+---------------------------+

在两列的输出中,值必须排列为COL_NAME

1 个答案:

答案 0 :(得分:0)

这不是你想做的吗?

select id, 'Name,Family,Phone' as col_name,
       concat(Name, ',', Family, ', ', Phone) as values
from t;

(注意:values是一个SQL关键字,因此它是列别名的错误名称。)