CONCAT(str1,str2,…)
方法
CONCAT(str1,str2,…)
说明
返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。如果所有参数均为非二进制字符串,则结果为非二进制字符串。 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如: SELECT CONCAT(CAST(int_col AS CHAR), char_col)。
代码如下:
mysql> select concat('exam','ple')
-> ;
+----------------------+
| concat('exam','ple') |
+----------------------+
| example |
+----------------------+
1 row in set (0.00 sec)
mysql> select concat(' ','Catch','a','failing','start');
-> ;
-> ^C
mysql> select concat(' ','Catch','a','failing','start');
+-------------------------------------------+
| concat(' ','Catch','a','failing','start') |
+-------------------------------------------+
| Catchafailingstart |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> select concat_ws(' ','Catch','a','failing','start');
+----------------------------------------------+
| concat_ws(' ','Catch','a','failing','start') |
+----------------------------------------------+
| Catch a failing start |
+----------------------------------------------+
1 row in set (0.00 sec)
mysql> select concat('My','NULL','QL');
+--------------------------+
| concat('My','NULL','QL') |
+--------------------------+
| MyNULLQL |
+--------------------------+
1 row in set (0.00 sec)
mysql> select concat('My',NULL,'QL');
+------------------------+
| concat('My',NULL,'QL') |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)
mysql> select concat(143.3)
-> ;
+---------------+
| concat(143.3) |
+---------------+
| 143.3 |
+---------------+
1 row in set (0.00 sec)
CONCAT_WS(separator,str1,str2,…)
方法
CONCAT_WS(separator,str1,str2,…)
说明
CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。 第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。
代码如下:
mysql> select concat_ws(',','First name','Second name','Last name');
+-------------------------------------------------------+
| concat_ws(',','First name','Second name','Last name') |
+-------------------------------------------------------+
| First name,Second name,Last name |
+-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select concat_ws(',','First name',NULL,'Last name');
+----------------------------------------------+
| concat_ws(',','First name',NULL,'Last name') |
+----------------------------------------------+
| First name,Last name |
+----------------------------------------------+
1 row in set (0.00 sec)
mysql> select concat_ws(',','First name',NULL,' ','Last name');
+--------------------------------------------------------+
| concat_ws(',','First name',NULL,' ','Last name') |
+--------------------------------------------------------+
| First name, ,Last name |
+--------------------------------------------------------+
1 row in set (0.00 sec)