我有一个像这样的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
提前致谢。
答案 0 :(得分:4)
您可以使用CONCAT()功能
SELECT id,
CONCAT(firstname,',',lastname) AS `name`
FROM table
还有CONCAT_WS
SELECT id,
CONCAT_WS(',',firstname,lastname) AS `name`
FROM table