在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:00:16  来源:igfitidea点击:

Concatenate multiple rows in single rows in MySQL

mysqlconcatenation

提问by iOSAppDev

How can I concatenate all the rows in single rows when I fire SELECT query?

当我触发 SELECT 查询时,如何将所有行连接到单行中?

enter image description here

在此处输入图片说明

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_concatand concatfunctions

使用group_concatconcat函数的组合

 SELECT group_concat( concat( id, " ",name," ",city," ",state," " ) SEPARATOR ' ')
 FROM tablename

回答by Vyktor

You'll need GROUP_CONCATand CONCATmysql functions and the query should look like this:

您将需要GROUP_CONCATCONCATmysql 函数,查询应如下所示:

SELECT GROUP_CONCAT( CONCAT( id, ' ', name, ' ', city, ' ', state) SEPARATOR ' ')
FROM students
GROUP BY (1)

Or you can use CONCAT_WSinstead:

或者您可以CONCAT_WS改用:

CONCAT_WS(' ', id, name, city, state)