需求

在发布系统中所有前置任务里面增加一些内容,发布系统中大约有200+的项目,手动是不可能手动的,只有在数据库中操作了。

思路

思路?既然操作数据库哪肯定得去看MySQL手册喽。

在Mysql手册中查找String相关资料,找到并进入String Functions,可以找到CONCAT和CONCAT_WS两个关于字符串拼接的函数文档链接
Mysql在字段现有内容后面追加内容

Mysql在字段现有内容后面追加内容

过程

CONCAT函数

官方解释:

Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent nonbinary string form. returns NULL if any argument is NULL.

返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。或许有一个或多个参数。 如果所有参数均为非二进制字符串,则结果为非二进制字符串。 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型。

函数格式:CONCAT(str1,str2,...)

mysql> select concat('lian','st');
+---------------------+
| concat('lian','st') |
+---------------------+
| lianst              |
+---------------------+
1 row in set (0.00 sec)

CONCAT_WS函数

CONCAT_WS函数与CONCAT函数大致相同,唯一的不同点是CONCAT_WS支持在两个字符串之间使用分隔符,本次解决问题就是使用的CONCAT_WS函数。

函数格式:CONCAT_WS(separator,str1,str2,...)

CONCAT_WS的第一个参数是分隔符:

  • char(10):换行符
  • char(13):回车符
mysql> select concat_ws(',','lian','st'); # 第一个参数是分隔符
+----------------------------+
| concat_ws(',','lian','st') |
+----------------------------+
| lian,st                    |
+----------------------------+
1 row in set (0.00 sec)

mysql> select concat_ws(char(10),'lian','st');  # 换行符
+---------------------------------+
| concat_ws(char(10),'lian','st') |
+---------------------------------+
| lian
st                         |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select concat_ws(char(13),'lian','st');  # 回车符
+---------------------------------+
| concat_ws(char(13),'lian','st') |
+---------------------------------+
st                         |
+---------------------------------+
1 row in set (0.00 sec)
文章目录