CONCAT() 和 CONCAT_WS() 函数有什么区别?

来源:这里教程网 时间:2026-02-28 17:11:25 作者:

concat() 和 concat_ws() 函数有什么区别?

CONCAT() 和 CONCAT_WS() 函数都用于连接两个或多个字符串,但它们之间的基本区别在于 CONCAT_WS() 函数可以与字符串之间的分隔符一起进行连接,而在 CONCAT() 函数中没有分隔符的概念。它们之间的其他重要区别是,如果任何参数为 NULL,CONCAT() 函数将返回 NULL,而如果分隔符为 NULL,CONCAT_WS() 函数将返回 NULL。

示例

下面的示例演示了 CONCAT() 和 CONCAT_WS() 函数之间的区别 -

mysql> Select CONCAT('Ram','is','a','good','student') AS 'Example of CONCAT()';
+---------------------+
| Example of CONCAT() |
+---------------------+
| Ramisagoodstudent   |
+---------------------+
1 row in set (0.00 sec)
mysql> Select CONCAT_WS(' ','Ram','is','a','good','student') AS 'Example of CONCAT_WS()';
+------------------------+
| Example of CONCAT_WS() |
+------------------------+
| Ram is a good student  |
+------------------------+
1 row in set (0.00 sec)

相关推荐