老邓
msgbartop
2012,在这操蛋的社会、操蛋的年代。-> 心神不宁!
msgbarbottom

17 Apr 12 [Mysql]Update Case When Then批量更新数据库操作

今天写一个批量自动化描述生成程序,为了提高更新效率,需使用批量更新数据库操作。批量update语句格式:

update 表 set 列= case 条件列
when 条件值1 then 更新为1
when 条件值2 then 更新为2
when 条件值3 then 更新为3
........
end
WHERE ....;

done.

04 Jul 10 [mysql]使用concat_ws联表update

批量修改某个或某几个字段内容,需要联合其他表的内容时可用concat_ws函数:

CONCAT_WS(separator,str1,str2,...)

CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.

mysql> SELECT CONCAT_WS(',','First name','Second name','Last Name');
-> 'First name,Second name,Last Name'
mysql> SELECT CONCAT_WS(',','First name',NULL,'Last Name');
-> 'First name,Last Name'
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

比如要批量更新 `产品表` 的 `产品描述` 字段,描述格式为 “产品名称_所属分类名称_网站名称” ,产品表的 `分类ID` 字段关联 `产品分类表` 的 `分类ID` 字段,除了用一大堆的语句加 while 来写,最简单的用concat_ws函数一句话搞定:

UPDATE `产品表`,`产品分类表` SET `产品表`.`产品描述`=concat_ws("_", `产品表`.`产品名称`, `产品分类表`.`分类名称`, "网站名称") WHERE `产品表`.`分类ID`=`产品分类表`.`分类ID`;

OK,大功告成。