簡體   English   中英

Concat單行mysql中的多行和多列

[英]Concat multiple row and column in single row mysql

我有一個包含以下格式數據的表

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中可能嗎?

一起使用group_concat和concat:

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

您可以這樣做以將“:”移到

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

這是hims056創建的更新的SQLFiddle

用這個:

 select concat(id, ':',name, ':', age) as total_concated 
 from your_table 
 where 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM