mysql实战之 批量update
mysql实战之批量update
现阶段我们的业务量很小,要对admin_user表中的relationship字段进行更新,指定id是409。已知409是公司内的一服务中心,需要把该服务中心放到区代下面, 区代id是3486,相关字段是paren_id(父子级别关系),需要把409已经409旗下所有商家以及客户都转移到3486下面,这个对应字段是relationship ,业务介绍完毕,下面来实战操作:
查看各个id现在的relationship情况
mysql> SELECT relationship FROM admin_user WHERE parent_id=409;
+-------------------------+
| relationship |
+-------------------------+
| ,1,30,304,405,409,437, |
| ,1,30,304,405,409,450, |
| ,1,30,304,405,409,464, |
| ,1,30,304,405,409,465, |
| ,1,30,304,405,409,471, |
| ,1,30,304,405,409,505, |
| ,1,30,304,405,409,540, |
| ,1,30,304,405,409,793, |
| ,1,30,304,405,409,794, |
| ,1,30,304,405,409,1801, |
| ,1,30,304,405,409,1802, |
| ,1,30,304,405,409,1949, |
| ,1,30,304,405,409,2015, |
+-------------------------+
13 rows in set (0.02 sec)
mysql> SELECT relationship FROM admin_user WHERE parent_id=3486;
+---------------------+
| relationship |
+---------------------+
| ,1,30,304,3486,409, |
+---------------------+
1 row in set (0.02 sec)
现在就是要把parent_id=409 的relationship中’,1,30,304,405,409,437,’所有的409换成3486,
笨方法:一条一条的update,但是业务量小,这样可以实现,假如是几万条数据的话这样操作是不可取的,还有手动更改某一个数很难保证正确性。
UPDATE admin_user SET relationship=',1,30,304,405,3486,437,' WHERE id=437;
UPDATE admin_user SET relationship=',1,30,304,405,3486,450,' WHERE id=450;
UPDATE admin_user SET relationship=',1,30,304,405,3486,464,' WHERE id=464;
。。。。。。。。。。。
其实我就是用这样的方法去更新的,后来经过领导指点采取新的方法实现一条语句更新来实现业务,
使用到的是连接函数concat,查看语法
mysql> help concat
Name: 'CONCAT'
Description:
Syntax:
CONCAT(str1,str2,...)
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.
CONCAT() returns NULL if any argument is NULL.
URL: http://dev.mysql.com/doc/refman/5.6/en/string-functions.html
Examples:
mysql> SELECT CONCAT('My', 'S', 'QL');
-> 'MySQL'
mysql> SELECT CONCAT('My', NULL, 'QL');
-> NULL
mysql> SELECT CONCAT(14.3);
-> '14.3'
我需要替换掉最左边之前的字符,都是固定的,先写下查询的sql语句试试,
mysql> SELECT CONCAT(',1,30,304,3486,409,',RIGHT(relationship,LENGTH(relationship)-LENGTH(',1,30,304,405,409,'))) AS r1,relationship FROM admin_user WHERE relationship LIKE ',1,30,304,405,409,%';
+--------------------------+-------------------------+
| r1 | relationship |
+--------------------------+-------------------------+
| ,1,30,304,3486,409,437, | ,1,30,304,405,409,437, |
| ,1,30,304,3486,409,450, | ,1,30,304,405,409,450, |
| ,1,30,304,3486,409,464, | ,1,30,304,405,409,464, |
| ,1,30,304,3486,409,465, | ,1,30,304,405,409,465, |
| ,1,30,304,3486,409,471, | ,1,30,304,405,409,471, |
| ,1,30,304,3486,409,505, | ,1,30,304,405,409,505, |
| ,1,30,304,3486,409,540, | ,1,30,304,405,409,540, |
| ,1,30,304,3486,409,793, | ,1,30,304,405,409,793, |
| ,1,30,304,3486,409,794, | ,1,30,304,405,409,794, |
| ,1,30,304,3486,409,1801, | ,1,30,304,405,409,1801, |
| ,1,30,304,3486,409,1802, | ,1,30,304,405,409,1802, |
| ,1,30,304,3486,409,1949, | ,1,30,304,405,409,1949, |
| ,1,30,304,3486,409,2015, | ,1,30,304,405,409,2015, |
+--------------------------+-------------------------+
13 rows in set (0.01 sec)
很显然,r1就是我想要的结果,下面可以更改成一个更新语句了,
mysql> update admin_user set relationship=concat(',1,30,304,3486,409,',RIGHT(relationship,LENGTH(relationship)-LENGTH(',1,30,304,405,409,'))) where relationship LIKE ',1,30,304,405,409,%';
Query OK, 13 rows affected (0.05 sec)
Rows matched: 13 Changed: 13 Warnings: 0
mysql> select relationship from admin_user where parent_id=409;
+--------------------------+
| relationship |
+--------------------------+
| ,1,30,304,3486,409,437, |
| ,1,30,304,3486,409,450, |
| ,1,30,304,3486,409,464, |
| ,1,30,304,3486,409,465, |
| ,1,30,304,3486,409,471, |
| ,1,30,304,3486,409,505, |
| ,1,30,304,3486,409,540, |
| ,1,30,304,3486,409,793, |
| ,1,30,304,3486,409,794, |
| ,1,30,304,3486,409,1801, |
| ,1,30,304,3486,409,1802, |
| ,1,30,304,3486,409,1949, |
| ,1,30,304,3486,409,2015, |
+--------------------------+
13 rows in set (0.02 sec)
搞定,期间我尝试过用when case来实现这个业务,跟单个update一样繁琐,就这13条数据也看不出来有多效率,所以还是这个拼接替换好用,菜鸟级水平奉上。
mysql实战之 批量update的更多相关文章
- mysql语句:批量更新多条记录的不同值[转]
mysql语句:批量更新多条记录的不同值 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 帮助 1 UPDATE mytable SET myfield = 'value' WHERE ...
- MyBatis魔法堂:各数据库的批量Update操作
一.前言 MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了.本篇仅记录批量update操作的sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了. ...
- centos mysql 实战 第一节课 安全加固 mysql安装
centos mysql 实战 第一节课 安全加固 mysql安装 percona名字的由来=consultation 顾问+performance 性能=per con a mysql ...
- mysql 如何提高批量导入的速度
mysql 如何提高批量导入的速度 最近一个项目测试,有几个mysql数据库的表数据记录达到了几十万条,在搭建测试环境 导入 测试数据时,十分慢.在网上搜索了一下,有下面一些方法可以加快mysql数据 ...
- MySQL实战 | 03 - 谁动了我的数据:浅析MySQL的事务隔离级别
原文链接:这一次,带你搞清楚MySQL的事务隔离级别! 使用过关系型数据库的,应该都事务的概念有所了解,知道事务有 ACID 四个基本属性:原子性(Atomicity).一致性(Consistency ...
- mybatis批量update操作的写法,及批量update报错的问题解决方法
mybatis的批量update操作写法很简单,如下: public interface YourMapper extends BaseMapper<YourExt> { void upd ...
- 极客时间 Mysql实战45讲 07讲行锁功过:怎么减少行锁对性能的影响笔记 极客时间
极客时间 Mysql实战45讲 07讲行锁功过:怎么减少行锁对性能的影响笔记 极客时间极客时间 Mysql实战45讲 07讲行锁功过:怎么减少行锁对性能的影响笔记 极客时间 笔记体会: 方案一,事务相 ...
- MySQL实战45讲学习笔记:第十五讲
一.引子 在今天这篇答疑文章更新前,MySQL 实战这个专栏已经更新了 14 篇.在这些文章中,大家在评论区留下了很多高质量的留言.现在,每篇文章的评论区都有热心的同学帮忙总结文章知识点,也有不少同学 ...
- MySQL实战45讲学习笔记:第三十九讲
一.本节概况 MySQL实战45讲学习笔记:自增主键为什么不是连续的?(第39讲) 在第 4 篇文章中,我们提到过自增主键,由于自增主键可以让主键索引尽量地保持递增顺序插入,避免了页分裂,因此索引更紧 ...
随机推荐
- iOS推送遇到的问题
1. 推送证书过期. 同事说iOS客户端不能推送消息,发现推送证书过期了,苹果的推送证书有效期是一年,推送证书过期后就不能使用推送服务了.解决办法:重新请求推送证书,导出p12文件,传给后台服务器就可 ...
- MVC4与JSON交互的知识总结
一:jquery传递JSON给MVC4后台 1.JSON传递单个参数给Controller某个Action方法 [前台js] $(document).ready(function () { var p ...
- 第三篇:白话tornado源码之请求来了
上一篇<白话tornado源码之待请求阶段>中介绍了tornado框架在客户端请求之前所做的准备(下图1.2部分),本质上就是创建了一个socket服务端,并进行了IP和端口的绑定,但是未 ...
- Oracle之nclob类型
此类型会严重影响查询效率,请少用: nclob字段在查询结果中显示为<NCLOB>,查看nclob类型的值方法有两种 a.可点开...查看具体数据 b.选择所有数据,右击复制到Excel, ...
- Reflection应用场景-利用反射机制将表单数据自动填充到JavaBean中
- spark编写word count
创建SparkContext对象的时候需要传递SparkConf对象,SparkConf至少需要包含spark.master和spark.app.name这两个参数,不然的话程序不能正常运行 obje ...
- TIJ——Chapter Seven:Reusing Classes
Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has ...
- jQuery瀑布流
- 利用zlib库进行zip解压
1:到zlib官网上下载zlib,本文下载的是1.2.8的版本. 2:进行./configure,然后make. 3:进入zlib库中的contrib/minizip/路径下make,生成的miniz ...
- 基于Yahoo网站性能优化的34条军规及自己的见解
1.尽量减少HTTP请求次数 终端用户响应的时间中,有80%用于下载各项内容,这部分时间包括下载页面中的图像.样式表.脚本.Flash等.通过减少页面中的元素可以减少HTTP请求的次数,这是提高网页速 ...