
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Concatenate rows on the basis of boolean values in another column with MySQL
To concatenate rows on the basis of boolean value in another column, use GROUP_CONCAT(). Let us first create a table. Here, we have set one of the columns “isValidUser” as BOOLEAN −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserMessage varchar(100), isValidUser boolean ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(UserMessage,isValidUser) values('Hi',true); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable(UserMessage,isValidUser) values('Hello',false); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(UserMessage,isValidUser) values('Good',true); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserMessage,isValidUser) values('Awesome !!!!!',true); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+---------------+-------------+ | Id | UserMessage | isValidUser | +----+---------------+-------------+ | 1 | Hi | 1 | | 2 | Hello | 0 | | 3 | Good | 1 | | 4 | Awesome !!!!! | 1 | +----+---------------+-------------+ 4 rows in set (0.03 sec)
Following is the query to concatenate rows on the basis of boolean values in another column. Here, we are concatenating corresponding records for boolean 1 value and same for boolean 0 −
mysql> select isValidUser,group_concat(UserMessage) from DemoTable group by isValidUser;
This will produce the following output −
+-------------+---------------------------+ | isValidUser | group_concat(UserMessage) | +-------------+---------------------------+ | 0 | Hello | | 1 | Hi,Good,Awesome !!!!! | +-------------+---------------------------+ 2 rows in set (0.07 sec)
- Related Articles
- Match column values on the basis of the other two column values in MySQL
- Perform MySQL UPDATE on the basis of DATE value in another column
- MySQL query to fetch only a single field on the basis of boolean value in another field
- Concatenate two values from the same column with different conditions in MySQL
- Concatenate the column values with separate text in MySQL and display in a single column
- Display custom text in a new column on the basis of null values in MySQL?
- Add a temporary column in MySQL where the values depend on another column?
- Add a new column and set values in it on the basis of conditions in MySQL?
- MySQL query to select three highest values and sort alphabetically on the basis of corresponding column with name
- Set custom messages on the basis of a column with student marks in MySQL
- Exclude rows based on column value when another duplicate column value is found in MySQL?
- How to concatenate all values of a single column in MySQL?
- Get all the records with two different values in another column with MySQL
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- Extract tuples with specified common values in another column in MySQL?

Advertisements