- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Store strings in variables and concatenate them to display them in a single column in MYSQL
For this, use CONCAT_WS() in MySQL. Let us first create a −
mysql> create table DemoTable1433 -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientFirstName varchar(20), -> ClientLastName varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1433(ClientFirstName,ClientLastName) values('David','Miller'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select −
mysql> select * from DemoTable1433;
This will produce the following output −
+----------+-----------------+----------------+ | ClientId | ClientFirstName | ClientLastName | +----------+-----------------+----------------+ | 1 | David | Miller | +----------+-----------------+----------------+ 1 row in set (0.00 sec)
Following is the query to store strings in variables and concatenate them −
mysql> set @concat_variable:=(select concat_ws('-',ClientFirstName,ClientLastName) from DemoTable1433); Query OK, 0 rows affected (0.00 sec)
mysql> select @concat_variable;
This will produce the following output −
+------------------+ | @concat_variable | +------------------+ | David-Miller | +------------------+ 1 row in set (0.00 sec)
- Related Articles
- Find duplicate column values in MySQL and display them
- 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 select different values from same column and display them in different columns with MySQL?
- Select multiple columns and display in a single column in MySQL?
- MySQL query to count the number of 0s and 1s from a table column and display them in two columns?
- Concatenate date and time from separate columns into a single column in MySQL
- Concatenate all the columns in a single new column with MySQL
- A single MySQL query to combine strings from many rows into a single row and display the corresponding User Id sum in another column?
- How to upload multiple files and store them in a folder with PHP?
- Select multiple sums with MySQL query and display them in separate columns?
- How to display a single quote text as a column value in MySQL?
- MySQL query to display the first alphabet from strings in a separate column
- Is it possible to divide records in both ascending and descending order in MySQL and display them alternatively?
- How to get available wifi networks and display them in a list in android

Advertisements