

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
New line separator doesn't work for group_concat function in MySQL? How to use it correctly?
To use new line separator in group_concat() function, follow the below syntax −
select group_concat(concat_ws(' ',yourColumnName1, yourColumnName2) SEPARATOR "\r\n") from yourTableName;
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20), LastName varchar(20) ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName,LastName) values('John','Smith'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FirstName,LastName) values('David','Miller'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName,LastName) values('John','Doe'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 1 | John | Smith | | 2 | David | Miller | | 3 | John | Doe | +----+-----------+----------+ 3 rows in set (0.00 sec)
Following is the query to work with new line separator for group_concat function −
mysql> select group_concat(concat_ws(' ',FirstName, LastName) SEPARATOR "\r\n") from DemoTable;
This will produce the following output &mnus;
+-------------------------------------------------------------------+ | group_concat(concat_ws(' ',FirstName, LastName) SEPARATOR "\r\n") | +-------------------------------------------------------------------+ | John Smith David Miller John Doe | +-------------------------------------------------------------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- MySQL system variable table_type doesn't work?
- Python3 - Why loop doesn't work?
- Expression in CASE WHEN Clause doesn't work in MySQL query?
- Resolve ERROR 1111 (HY000): Invalid use of group function in MySQL? How to correctly use aggregate function with where clause?
- How to use GROUP BY to concatenate strings in MySQL and how to set a separator for the concatenation?
- How to use % wildcard correctly in MySQL?
- CSS content: attr() on HTML5 progress doesn't work
- How to use GROUP_CONCAT in CONCAT in MySQL?
- Getting last value in MySQL group concat?
- How to correctly use WITH ROLLUP in MySQL?
- How to create JSON format with group-concat in MySQL?
- MySQL query to group concat distinct by Id?
- How to add http:// if it doesn't exist in the URL PHP?
- Line Separator in Java
- HTML5 meta name = “viewport” doesn't work as expected
Advertisements