SelectId,Name,Address,CONCAT(ID,',',Name ,',',Address)AS'ID,Name,Address'fromStude"/> SelectId,Name,Address,CONCAT(ID,',',Name ,',',Address)AS'ID,Name,Address'fromStude">

Home  >  Article  >  Database  >  How can we combine the values ​​of two or more columns of a MySQL table and get that value in a single column?

How can we combine the values ​​of two or more columns of a MySQL table and get that value in a single column?

WBOY
WBOYforward
2023-09-12 12:57:081184browse

我们如何组合 MySQL 表的两列或多列的值并在单个列中获取该值?

To combine the values ​​of two or more columns, we can use the MySQL CONCAT() function. In this case, the argument to the CONCAT() function will be the name of the column. For example, let's say we have a table called "Students" and we want the students' names and addresses in one column, then 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 of a MySQL table and get that value in a single column?. 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