开发者

String concatenation in MySQL

I am using MySQL and MySQL Workbench 5.2 CE. When I try to concatenate 2 columns, last_name and first_name, it doesn't work :

select 开发者_运维百科first_name + last_name as "Name" from test.student


MySQL is different from most DBMSs' use of + or || for concatenation. It uses the CONCAT function:

SELECT CONCAT(first_name, ' ', last_name) AS Name FROM test.student

There's also the CONCAT_WS (Concatenate With Separator) function, which is a special form of CONCAT():

SELECT CONCAT_WS(' ', first_name, last_name) from test.student

That said, if you want to treat || as a string concatenation operator (same as CONCAT()) rather than as a synonym for OR in MySQL, you can set the PIPES_AS_CONCAT SQL mode.


Try:

select concat(first_name,last_name) as "Name" from test.student

or, better:

select concat(first_name," ",last_name) as "Name" from test.student


Use concat() function instead of + like this:

select concat(firstname, lastname) as "Name" from test.student


Apart from concat you can also use concat_ws (concatenate with separator):

SELECT CONCAT_WS(' ', first_name, last_name) from test.student

This function has the added benefit of skipping null values.

See https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws


That's not the way to concat in MYSQL. Use the CONCAT function Have a look here: http://dev.mysql.com/doc/refman/4.1/en/string-functions.html#function_concat

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜