MySQL query to group concat and place data into a single row on the basis of 1 values in corresponding column?


For this, use GROUP_CONCAT(). For only 1 values, work with MySQL WHERE clause. Let us first create a table −

mysql> create table DemoTable
(
   PlayerName varchar(40),
   PlayerStatus tinyint(1)
);
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris',1);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('David',0);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('Sam',1);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('Carol',1);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('Bob',0);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+--------------+
| PlayerName | PlayerStatus |
+------------+--------------+
| Chris      |            1 |
| David      |            0 |
| Sam        |            1 |
| Carol      |            1 |
| Bob        |            0 |
+------------+--------------+
5 rows in set (0.00 sec)

Following is the query to group concat and place data into a single row on the basis of 1 values in corresponding column −

mysql> select group_concat(PlayerName) from DemoTable where PlayerStatus=1;

This will produce the following output −

+--------------------------+
| group_concat(PlayerName) |
+--------------------------+
| Chris,Sam,Carol          |
+--------------------------+
1 row in set (0.00 sec)

Updated on: 09-Oct-2019

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements