
- 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
How to quote values of single column using GROUP_CONCAT and CONCAT with DISTINCT in MySQL?
For this, you can use group_concat() along with replace(). Let us first create a table −
mysql> create table DemoTable1799 ( EmployeeId varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1799 values('101,102,103,104'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1799 values('106,109'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1799;
This will produce the following output:
+-----------------+ | EmployeeId | +-----------------+ | 101,102,103,104 | | 106,109 | +-----------------+ 2 rows in set (0.00 sec)
Here is the query to quote values of single column using group_concat and concat with distinct −
mysql> select group_concat(distinct concat("'", replace(EmployeeId, "," , "','") , "'")) as Output from DemoTable1799;
This will produce the following output −
+-------------------------------------+ | Output | +-------------------------------------+ | '101','102','103','104','106','109' | +-------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to group concat distinct by Id?
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- MySQL query to group concat and place data into a single row on the basis of 1 values in corresponding column?
- How to create JSON format with group-concat in MySQL?
- Getting last value in MySQL group concat?
- Add to existing value in MySQL column using CONCAT function?
- How to concat Values in MySQL Query and to handle Null values as well?
- MySQL CONCAT a specific column value with the corresponding record
- How can I update MySQL table after quoting the values of a column with a single quote?
- How to use GROUP_CONCAT in CONCAT in MySQL?
- Select distinct values from three columns and display in a single column with MySQL
- Perform complex MySQL insert by using CONCAT()?
- Use MySQL concat() and lower() effectively
- MySQL concat() to create column names to be used in a query?
- How to quote values using MySQL group_concat?

Advertisements