在 MySQL 中的单行中连接多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9116501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate multiple rows in single rows in MySQL
提问by iOSAppDev
How can I concatenate all the rows in single rows when I fire SELECT query?
当我触发 SELECT 查询时,如何将所有行连接到单行中?
I want O/P like
我想要像 O/P
101 abc CA USA 102 xyz PH UK 103 pqr WDC EU
101 abc CA 美国 102 xyz PH 英国 103 pqr WDC 欧盟
Any help kindly appreciated. Thanks
任何帮助表示赞赏。谢谢
回答by
Use conbination of group_concat
and concat
functions
使用group_concat
和concat
函数的组合
SELECT group_concat( concat( id, " ",name," ",city," ",state," " ) SEPARATOR ' ')
FROM tablename
回答by Vyktor
You'll need GROUP_CONCAT
and CONCAT
mysql functions and the query should look like this:
您将需要GROUP_CONCAT
和CONCAT
mysql 函数,查询应如下所示:
SELECT GROUP_CONCAT( CONCAT( id, ' ', name, ' ', city, ' ', state) SEPARATOR ' ')
FROM students
GROUP BY (1)
Or you can use CONCAT_WS
instead:
或者您可以CONCAT_WS
改用:
CONCAT_WS(' ', id, name, city, state)