Board logo

标题: mysql实用字符串函数concat和concat_ws的区别 [打印本页]

作者: fangzhen    时间: 2010-12-29 13:43     标题: mysql实用字符串函数concat和concat_ws的区别

1、concat()函数
    1.1 MySQL的concat函数可以连接一个或者多个字符串
,
        mysql
> select concat('10');
        +--------------+
        |
concat('10') |
        +--------------+
        |
10           |
        +--------------+
       
1 row in set (0.00 sec)

       
mysql> select concat('11','22','33');
        +------------------------+
        |
concat('11','22','33') |
        +------------------------+
        |
112233                 |
        +------------------------+

       
1 row in set (0.00 sec)

1.2 MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL

        mysql
> select concat('11','22',null);
        +------------------------+
        |
concat('11','22',null) |
        +------------------------+
        |
NULL                   |
        +------------------------+
       
1 row in set (0.00 sec)
2、concat_ws()函数, 表示concat with separator,即有分隔符的字符串连接
    如连接后以逗号分隔
        mysql
> select concat_ws(',','11','22','33');

        +-------------------------------+
        |
concat_ws(',','11','22','33') |
        +-------------------------------+
        |
11,22,33                      |
        +-------------------------------+
       
1 row in set (0.00 sec)

   
和concat不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
        mysql
> select concat_ws(',','11','22',NULL);
        +-------------------------------+
        |
concat_ws(',','11','22',NULL) |
        +-------------------------------+
        |
11,22                         |
        +-------------------------------+
       
1 row in set (0.00 sec)





欢迎光临 PHP开发笔记 (http://phpvi.com/) Powered by Discuz! 6.1.0