我正在使用MySQL和MySQL Workbench 5.2 CE。当我尝试将两列last_name
和first_name
连接起来时,它不起作用:
select first_name + last_name as "Name" from test.student
我正在使用MySQL和MySQL Workbench 5.2 CE。当我尝试将两列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
(带分隔符连接)函数,它是CONCAT()
的一种特殊形式:
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
话虽如此,如果你想把 ||
当作字符串连接操作符(与在 MySQL 中使用 CONCAT()
一样),而不是作为 OR
的同义词,你可以设置 PIPES_AS_CONCAT
SQL 模式。
尝试:
select concat(first_name,last_name) as "Name" from test.student
或者,更好的方法:
select concat(first_name," ",last_name) as "Name" from test.student
MYSQL中连接字符串的方法不是那样的。使用CONCAT函数。这里可以查看: http://dev.mysql.com/doc/refman/4.1/en/string-functions.html#function_concat
Doctrine
的人,我不得不在CONCAT
中使用单引号来表示空格,并在整个查询周围使用双引号。 - cranedPIPES_AS_CONCAT
:SET @@SQL_MODE = CONCAT(@@SQL_MODE, ',PIPES_AS_CONCAT');
。取消设置:SET @@SQL_MODE = REPLACE(@@SQL_MODE, 'PIPES_AS_CONCAT', '');
。更多示例请参见 https://mariadb.com/kb/en/library/sql-mode/#examples 。 - mivk