
- 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 some of the column values in a single line with MySQL and prefix a particular string “MR.” to every string
The CONCAT() method would be used to concatenate “MR” to every string, whereas GROUP_CONCAT() to concatenate some of the column values in a single line.
Let us first see an example and create a table −
mysql> create table DemoTable799( UserId int, UserName varchar(100), UserAge int ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable799 values(101,'John',21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable799 values(102,'Chris',26); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable799 values(101,'Robert',23); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable799 values(103,'David',24); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable799 values(101,'Mike',29); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable799;
This will produce the following output −
+--------+----------+---------+ | UserId | UserName | UserAge | +--------+----------+---------+ | 101 | John | 21 | | 102 | Chris | 26 | | 101 | Robert | 23 | | 103 | David | 24 | | 101 | Mike | 29 | +--------+----------+---------+ 5 rows in set (0.00 sec)
Here is the query to concatenate strings −
mysql> select UserId,GROUP_CONCAT(CONCAT('MR.', UserName)) from DemoTable799 group by UserId;
This will produce the following output −
+--------+---------------------------------------+ | UserId | GROUP_CONCAT(CONCAT('MR.', UserName)) | +--------+---------------------------------------+ | 101 | MR.John,MR.Robert,MR.Mike | | 102 | MR.Chris | | 103 | MR.David | +--------+---------------------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Concatenate the column values with separate text in MySQL and display in a single column
- How to concatenate all values of a single column in MySQL?
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- Concatenate all the columns in a single new column with MySQL
- MySQL add “prefix” to every column?
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- How can we update MySQL table after removing a particular string from the values of column?
- Concatenate string with numbers in MySQL?
- Concatenate a string in an already created field values set with MySQL TEXT data type
- How to find string count of a particular id in a column using a MySQL query?
- Return only the first 15 characters from a column with string values in MySQL
- How to find and replace string in MySQL database for a particular string only?
- Concatenate date and time from separate columns into a single column in MySQL
- Join Map values into a single string with JavaScript?
- How to check if a string starts with a specified Prefix string in Golang?

Advertisements