如何在SQL中将两列合并为单列?

时间:2014-03-30 09:45:35

标签: mysql sql

我有一个像这样的SQL表

id | firstname | lastname
--------------------------
1  | alex      | smith
2  | bush      | hall
3  | cris      | cruise
4  | diana     | krall

我需要两列是“firstname”和“lastname”将合并成一列是“name”,而逗号会将它们分开,如下所示:

id | name
----------------
1  | alex, smith
2  | bush, hall
3  | cris, cruise
4  | diana, krall

提前致谢。

1 个答案:

答案 0 :(得分:4)

您可以使用CONCAT()功能

SELECT id,
CONCAT(firstname,',',lastname) AS `name`
FROM table

Fiddle Demo

还有CONCAT_WS

SELECT id,
CONCAT_WS(',',firstname,lastname) AS `name`
FROM table

Fiddle