Home >Database >Mysql Tutorial >How Does MySQL Concatenate Strings?
Concatenating Strings in MySQL
In MySQL, concatenating strings differs slightly from common practices in other Database Management Systems (DBMSs). Unlike using operators such as or ||, MySQL utilizes the dedicated CONCAT function for string concatenation.
To concatenate the last_name and first_name columns, the following query should be used:
SELECT CONCAT(first_name, ' ', last_name) AS Name FROM test.student
This command employs the CONCAT function to merge the content of the first_name and last_name columns into a new Name column.
Additionally, MySQL provides the CONCAT_WS (Concatenate With Separator) function as a specialized version of CONCAT(). It allows for the inclusion of a separator character between concatenated strings:
SELECT CONCAT_WS(' ', first_name, last_name) from test.student
In the example above, a space character is used as the separator.
MySQL also offers an alternative method for performing string concatenation through the PIPES_AS_CONCAT SQL mode. Activating this mode permits the usage of || as a string concatenation operator, equivalent to the CONCAT() function:
SET PIPES_AS_CONCAT=1; SELECT first_name || last_name AS Name FROM test.student;
The above is the detailed content of How Does MySQL Concatenate Strings?. For more information, please follow other related articles on the PHP Chinese website!