Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

group_concat MYSQL new line

Tags:

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

then using this php for displaying

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

using this query returns Name X,Y

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

Any idea?

like image 821
buni Avatar asked Nov 27 '11 10:11

buni


People also ask

What is the difference between concat and Group_concat in MySQL?

The difference here is while CONCAT is used to combine values across columns, GROUP_CONCAT gives you the capability to combine values across rows. It's also important to note that both GROUP_CONCAT and CONCAT can be combined to return desired results.

What does Group_concat do in MySQL?

GROUP_CONCAT is a function which concatenates/merges the data from multiple rows into one field. It is a GROUP BY function which returns a string if the group contains at least 1 non-null value, if it does not, it returns a Null value.

Is there a limit to Group_concat?

Show activity on this post. I'm using GROUP_CONCAT() in a MySQL query to convert multiple rows into a single string. However, the maximum length of the result of this function is 1024 characters.

Can we use Group_concat in SQL Server?

The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.


2 Answers

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

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

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

like image 187
shadyyx Avatar answered Jan 02 '23 08:01

shadyyx


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, 
like image 39
buni Avatar answered Jan 02 '23 09:01

buni