我们如何将 MySQL 表中两列或者更多列的值合并到一列中?
我们可使用 MySQL CONCAT() 函数来合并两列或更多列的值。在本例中,CONCAT() 函数的参数为列名。举例来说,假设我们有一个名为“Student”的表,并且想将学生的名字和地址合并到一列中,那么我们可编写如下查询:
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)
广告