Home >Database >Mysql Tutorial >How to use the CONCAT() function in MySQL

How to use the CONCAT() function in MySQL

王林
王林forward
2023-05-29 12:10:063913browse

CONCAT()

CONCAT(str1,str2,…))The function is used to return a string after concatenating multiple strings, for example:

SELECT CONCAT('MySQL', '字符串', '函数') AS str;
str           |
--------------+
MySQL字符串函数|

If any parameter in this function is NULL, the return result is NULL. For example:

SELECT CONCAT('MySQL', NULL, '函数') AS str;
str|
---+
   |

For string constants, we can also write them together directly. For example:

SELECT 'MySQL' '字符串' '函数' AS str;
str           |
--------------+
MySQL字符串函数|

The above method can only be used to connect string constants and cannot be used to connect field values.

?If SQL mode PIPES_AS_CONCAT is enabled, the MySQL logical OR operator (||) can also be used to concatenate strings, similar to Oracle and PostgreSQL.

In addition to the CONCAT(str1,str2,…)) function, CONCAT_WS(separator,str1,str2,…))function means using the specified separator separator to connect multiple string, if the delimiter is NULL, NULL is returned. For example:

SELECT CONCAT_WS('-', 'MySQL', NULL, '字符串') AS str1,
       CONCAT_WS(NULL, 'MySQL', '字符串') AS str2;
str1       |str2|
-----------+----+
MySQL-字符串|    |

The above is the detailed content of How to use the CONCAT() function in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete