MySQL query to return all items in a single row


For this, use GROUP_CONCAT(). Let us first create a table−

mysql> create table DemoTable1355
    -> (
    -> Location text
    -> );
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1355 values('E:');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1355 values('AllPrograms');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1355 values('ChatApplicationInJava');
Query OK, 1 row affected (0.38 sec)
mysql> insert into DemoTable1355 values('MainFolder');
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1355;

This will produce the following output −

+-----------------------+
| Location              |
+-----------------------+
| E:                    |
| AllPrograms           |
| ChatApplicationInJava |
| MainFolder            |
+-----------------------+
4 rows in set (0.00 sec)

Here is the query to return all items in one row −

mysql> select group_concat(Location separator '/') from DemoTable1355;

This will produce the following output −

+-------------------------------------------------+
| group_concat(Location separator '/')            |
+-------------------------------------------------+
| E:/AllPrograms/ChatApplicationInJava/MainFolder |
+-------------------------------------------------+
1 row in set (0.00 sec)

Updated on: 05-Nov-2019

767 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements