Concatenating Strings in MySQL
While attempting to merge two columns, last_name and first_name, a user encountered an issue in MySQL and MySQL Workbench 5.2 CE. The following query failed to concatenate the columns:
select first_name + last_name as "Name" from test.student
Understanding String Concatenation in MySQL
Unlike many other database management systems (DBMSs), MySQL does not use the or || operators for string concatenation. Instead, it employs the CONCAT function:
SELECT CONCAT(first_name, ' ', last_name) AS Name FROM test.student
MySQL also provides the CONCAT_WS (Concatenate With Separator) function, a variant of CONCAT() that allows specifying a separator between the concatenated values:
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
Additional Considerations
For users seeking to interpret || as a string concatenation operator in MySQL, the PIPES_AS_CONCAT SQL mode can be enabled to replicate the behavior of CONCAT(). However, it's important to note that this mode affects the behavior of || for all queries, not just string concatenation operations.
The above is the detailed content of How to Concatenate Strings in MySQL?. For more information, please follow other related articles on the PHP Chinese website!