2

我有一个包含以下格式数据的表格

id    |    name   |   age
----------------------------
1     |    John   |   24
2     |    Sam    |   NULL
3     |    Ron    |   32
4     |    Harry  |   44

现在我想把它放到一行中

1:John:24,2:Sam,3:Ron:32,4:Harry:44

我试过 group_concat 但它只给了我一列用逗号分隔的值,在 mysql 中是否可能?

4

2 回答 2

4

一起使用 group_concat 和 concat:

SELECT group_concat(concat(id, ':', name, ':', IFNULL(age,''))) 
FROM table1

您可以这样做以使“:”移过来

SELECT group_concat(concat(id, ':', name, IFNULL(concat(':',age),''))) 
FROM table1

这是 hiss056 创建的更新的SQLFiddle 。

于 2013-05-09T11:26:32.570 回答
0

用这个:

 select concat(id, ':',name, ':', age) as total_concated 
 from your_table 
 where 1
于 2013-05-09T11:23:48.043 回答