Home  >  Article  >  Database  >  How can we combine the values ​​of two or more columns in a MySQL table?

How can we combine the values ​​of two or more columns in a MySQL table?

王林
王林forward
2023-09-16 10:13:06643browse

How can we combine the values ​​of two or more columns in a MySQL table?

For merging the values ​​of two or more columns of a MySQL table, we can use the CONCAT() string function. Basically, MySQL CONCAT() function is used to merge two or more strings.

Syntax

CONCAT(String1,String2,…,StringN)

Here, the parameter of the CONCAT function is the string that needs to be merged.

Example

mysql> select CONCAT('Ram','is','a','good','boy') AS Remarks;
+---------------+
| Remarks       |
+---------------+
| Ramisagoodboy |
+---------------+
1 row in set (0.00 sec)

Similarly, we can use the CONCAT() function to combine the values ​​of two or more columns. For example, assuming we have a table called "Student" and we want to group the students' names and addresses in one column, we can write the following query:

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)

The above is the detailed content of How can we combine the values ​​of two or more columns in a MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete