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. +---------------------------------+
  8. mysql> select concat('-','aa','-','bb','-','cc');
  9. +------------------------------------+
  10. | concat('-','aa','-','bb','-','cc') |
  11. +------------------------------------+
  12. | -aa-bb-cc |
  13. +------------------------------------+
  14. 1 row in set (0.00 sec)
  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

0x03 group_concat ()

每个字段必须指定分隔符,去拼接字符串

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

  1. mysql> select group_concat('-','aa','-','bb','-','cc');
  2. +------------------------------------------+
  3. | group_concat('-','aa','-','bb','-','cc') |
  4. +------------------------------------------+
  5. | -aa-bb-cc |
  6. +------------------------------------------+
  7. 1 row in set (0.00 sec)