简体   繁体   中英

How to merge two column into single column in SQL?

I have a SQL table like this

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

And i need two column are "firstname" and "lastname" will merge into a column is "name", and a comma will separated them, like this:

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

Thanks in advance.

You can use CONCAT() function

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

Fiddle Demo

Also with CONCAT_WS

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

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM