0x01 concat()

注意:返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null

用途:连接一个或多个字符串
语法:concat(str1,str2…)

  1. # 不带NULL的
  2. mysql> select concat('123','456','aaa','789');
  3. +---------------------------------+
  4. | concat('123','456','aaa','789') |
  5. +---------------------------------+
  6. | 123456aaa789 |
  7. +---------------------------------+
  1. # 带NULL的
  2. mysql> select concat('123','456',NULL,'aaa','789');
  3. +--------------------------------------+
  4. | concat('123','456',NULL,'aaa','789') |
  5. +--------------------------------------+
  6. | NULL |
  7. +--------------------------------------+
  8. 1 row in set

0x02 concat_ws()

注意:如果 concat_ws 的参数有NULL会无视掉

用途:使用固定连接符连接一个或多个字符串
语法:concat_ws(separator,str1,str2…)

  1. # 不带NULL的
  2. mysql> select concat_ws('-','aa','bb','cc');
  3. +------------------------+
  4. | concat_ws('-','aa','bb','cc') |
  5. +------------------------+
  6. | aa-bb-cc |
  7. +------------------------+
  8. 1 row in set
  1. # 带NULL的
  2. mysql> select concat_ws('-','aa','bb',NULL,'cc');
  3. +-----------------------------+
  4. | concat_ws('-','aa','bb',NULL,'cc') |
  5. +-----------------------------+
  6. | aa-bb-cc |
  7. +-----------------------------+
  8. 1 row in set