
- 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 can we combine the values of two or more columns of MySQL table?
For combining the values of two or more columns of MySQL table, we can use CONCAT() string function. Basically, MySQL CONCAT() function is used to combine two or more strings.
Syntax
CONCAT(String1,String2,…,StringN)
Here, the arguments of CONCAT functions are the strings that need to be combined.
Example
mysql> select CONCAT('Ram','is','a','good','boy') AS Remarks; +---------------+ | Remarks | +---------------+ | Ramisagoodboy | +---------------+ 1 row in set (0.00 sec)
Similarly, we can use CONCAT() function to combine the values of two or more columns. For example, suppose we have a table named ‘Student’ and we want the name and address of the student collectively in one column then the following query can be written −
mysql> Select Id, Name, Address, CONCAT(ID,', ',Name,', ', Address)AS 'ID, Name, Address' from Student; +------+---------+---------+--------------------+ | Id | Name | Address | ID, Name, Address | +------+---------+---------+--------------------+ | 1 | Gaurav | Delhi | 1, Gaurav, Delhi | | 2 | Aarav | Mumbai | 2, Aarav, Mumbai | | 15 | Harshit | Delhi | 15, Harshit, Delhi | | 20 | Gaurav | Jaipur | 20, Gaurav, Jaipur | +------+---------+---------+--------------------+ 4 rows in set (0.00 sec)
- Related Articles
- How can we combine values of two or more columns of MySQL table and get that value in a single column?
- How can we add values into the columns of a MySQL table?
- How can we fetch one or more columns as output from a MySQL table?
- How can we list all the columns of a MySQL view as we can list the columns of a MySQL table?
- How can we get more details about columns of an existing table than return by MySQL SHOW COLUMNS statement?
- In MySQL, how can I combine two or more strings along with a separator?
- How we can get more information about columns of a table than the\ninformation we got by DESCRIBE, EXPLAIN and SHOW COLUMNS MySQL statements?
- How can we add columns with default values to an existing MySQL table?
- How can we update the values in one MySQL table by using the values of another MySQL table?
- How can I return the values of columns from MySQL table as a set of values?
- How to find the minimum values of two or more fields in MySQL?
- How can we get randomly different set of rows or values each time from MySQL table?
- How can we use LPAD() or RPAD() functions with the values in the column of a MySQL table?
- How can we get a list of columns in an existing MySQL table?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?

Advertisements