我正在使用 MySQL 和 MySQL Workbench 5.2 CE。当我尝试连接 2 列时last_name,first_name它不起作用:
select first_name + last_name as "Name" from test.student
我正在使用 MySQL 和 MySQL Workbench 5.2 CE。当我尝试连接 2 列时last_name,first_name它不起作用:
select first_name + last_name as "Name" from test.student
MySQL 不同于大多数 DBMS 对连接的使用+或||连接。它使用以下CONCAT功能:
SELECT CONCAT(first_name, ' ', last_name) AS Name FROM test.student
还有CONCAT_WS(Concatenate With Separator) 函数,它是 的一种特殊形式CONCAT():
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
也就是说,如果您想将||其视为字符串连接运算符(与 相同CONCAT())而不是ORMySQL 中的同义词,您可以设置PIPES_AS_CONCATSQL 模式。
尝试:
select concat(first_name,last_name) as "Name" from test.student
或更好:
select concat(first_name," ",last_name) as "Name" from test.student
使用concat()函数而不是+这样:
select concat(firstname, lastname) as "Name" from test.student
这不是在 MYSQL 中连接的方式。使用 CONCAT 函数看看这里:http ://dev.mysql.com/doc/refman/4.1/en/string-functions.html#function_concat
除了concat你还可以使用concat_ws(用分隔符连接):
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
此函数具有跳过null值的额外好处。
见https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws