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

How to use the CONCAT() function in MySQL

王林
Release: 2023-05-29 12:10:06
forward
3937 people have browsed it

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字符串函数|
Copy after login

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

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

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

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

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-字符串|    |
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template