php group_concat MYSQL 换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8285035/
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-26 04:21:15  来源:igfitidea点击:

group_concat MYSQL new line

phpmysql

提问by buni

group_concat(A,' ',B,' ',C) as Name,

then using this php for displaying

然后使用这个 php 进行显示

<td><?php echo $row['Name']; ?></td>

using this query returns Name X,Y

使用此查询返回 Name X,Y

but i prefer to have the names not comma separated rather line break X then Y new line

但我更喜欢名称不是逗号分隔,而是换行 X 然后 Y 换行

Any idea?

任何的想法?

采纳答案by Mahmoud Gamal

I don't understand what do you mean by line break between X and Y, but if you need the values to be comma separated you can add any separator to the Group_Concatlike that:

我不明白 X 和 Y 之间的换行符是什么意思,但是如果您需要以逗号分隔的值,您可以像这样向Group_Concat添加任何分隔符:

group_concat(Name SEPARATOR ' ') as Name

and here is some other separatorsyou can use.

这是您可以使用的其他一些分隔符

回答by shadyyx

For MySQL (or plain text) output You could use \nas a separator:

对于 MySQL(或纯文本)输出,您可以\n用作分隔符:

SELECT GROUP_CONCAT(column1 SEPARATOR '\n') FROM table1;

I use this very often when I need to get many new-line-separatedvalues in one row for other processing.

当我需要在一行中获取许多换行分隔值以进行其他处理时,我经常使用它。

回答by buni

I figured it out. this is the correct way to add line break as seperator in the browser:

我想到了。这是在浏览器中添加换行符作为分隔符的正确方法:

group_concat(A,' ',B,' ',C separator '<br>') as Name,

回答by Ashutosh SIngh

Example

例子

SELECT Name, GROUP_CONCAT(city SEPARATOR '\n') AS city, nb
FROM
(SELECT 'A' AS Name, 'Agra' AS city, 101 AS nb
UNION ALL
SELECT 'B' AS Name, 'Delhi' AS city, 102 AS nb
UNION ALL
SELECT 'A' AS Name, 'Allahabad' AS city, 101 AS nb) AS a
GROUP BY Name, nb;