
- 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 concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash
For this, you can use GROUP_CONCAT().
Let us first create a table −
mysql> create table DemoTable764 ( ProductId int, ProductPrice int ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable764 values(101,10000); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable764 values(102,1090); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable764 values(103,4000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable764 values(102,3450); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable764 values(101,20000); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable764 values(104,50000); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable764;
This will produce the following output -
+-----------+--------------+ | ProductId | ProductPrice | +-----------+--------------+ | 101 | 10000 | | 102 | 1090 | | 103 | 4000 | | 102 | 3450 | | 101 | 20000 | | 104 | 50000 | +-----------+--------------+ 6 rows in set (0.00 sec)
Following is the query to concatenate columns with corresponding duplicate id values −
mysql> select ProductId, group_concat(ProductPrice SEPARATOR '/') AS ProductPrice from DemoTable764 group by ProductId;
This will produce the following output -
+-----------+---------------+ | ProductId | ProductPrice | +-----------+---------------+ | 101 | 10000/20000 | | 102 | 1090/3450 | | 103 | 4000 | | 104 | 50000 | +-----------+---------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to combine duplicate values into one with corresponding value separated by hyphens in MySQL?
- MySQL query to count the duplicate ID values and display the result in a separate column
- Find duplicate column values in MySQL and display them
- MySQL query to fetch the maximum corresponding value from duplicate column values
- Add records from corresponding duplicate values in another column with MySQL
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- Find average on the basis of corresponding duplicate VARCHAR values in MySQL
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Find and display duplicate values only once from a column in MySQL
- MySQL query to concatenate all the values in each row based on the common matching ID
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- How to order return duplicate column values only once in MySQL?
- Display all the column values in a single row separated by comma in MySQL?
- Update all varchar column rows to display values before slash in MySQL?

Advertisements