mysql 多字段更新
更新一个字段当好写
update user set collect_num=(select sum(collect_num) from article where user_id=user.id) where user.id=1;
Query OK, 0 rows affected (17.36 sec)
Rows matched: 1 Changed: 0 Warnings: 0
问题是想更新多个字段
sql server 支持下面这种语法
update user set (article_num,collect_num,like_num)=(select count(*),sum(collect_num),sum(like_num) from article where user_id=user.id) where user.id=1;
试过并查官网后,发现mysql并不支持
先用最笨的办法
update user set
article_num=(select count(*) from article where user_id=user.id),
collect_num=(select sum(collect_num) from article where user_id=user.id),
like_num=(select sum(collect_num) from article where user_id=user.id)
where user.id=1;
Query OK, 1 row affected (54.79 sec)
Rows matched: 1 Changed: 1 Warnings: 0
时间居然是更新一个字段的的3倍,可能是查了三次article表,验证确实如此
mysql> explain select (select count(*) from article where user_id=1),(select sum(collect_num) from article where user_id=1),(select sum(collect_num) from article where user_id=1);
+----+-------------+---------+-------+------------------------------+------------------------------+---------+------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+------------------------------+------------------------------+---------+------+---------+--------------------------+
| 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| 4 | SUBQUERY | article | index | user_id_like_num_collect_num | user_id_like_num_collect_num | 104 | NULL | 3047331 | Using where; Using index |
| 3 | SUBQUERY | article | index | user_id_like_num_collect_num | user_id_like_num_collect_num | 104 | NULL | 3047331 | Using where; Using index |
| 2 | SUBQUERY | article | index | user_id_like_num_collect_num | user_id_like_num_collect_num | 104 | NULL | 3047331 | Using where; Using index |
+----+-------------+---------+-------+------------------------------+------------------------------+---------+------+---------+--------------------------+
不能忍
能想到的办法就是起临时表(或外部写代码,python什么的,实现类似临时表的功能)了,毕竟临时表万能。
但还是想在sql层面解决
update user u
JOIN (select user_id as user_id,count(*) as article_num,sum(collect_num) as collect_num,sum(like_num) as like_num from article where user_id=1) t
on u.id=t.user_id
set u.article_num=t.article_num,u.collect_num=t.collect_num,u.like_num=t.like_num
where u.id=1;
虽然代码不如sqlserver 漂亮,需要改两个值,不过达到目的了,时间并不比单字段耗时。
更改为单查询条件
update user u
JOIN (select user_id as user_id,count(*) as article_num,sum(collect_num) as collect_num,sum(like_num) as like_num
from article
group by user_id) t
on u.id=t.user_id
set u.article_num=t.article_num,u.collect_num=t.collect_num,u.like_num=t.like_num where u.id=101;
总算差不多了
个人最熟悉sqlserver,mysql只是顺带打酱油的,如果mysql有更有效的办法,还望不吝告知。
mysql 多字段更新的更多相关文章
- EFCore+Mysql仓储层建设(分页、多字段排序、部分字段更新)
前沿 园子里已有挺多博文介绍了EFCore+Mysql/MSSql如何进行使用,但实际开发不会把EF层放在Web层混合起来,需要多个项目配合结构清晰的进行分层工作,本文根据个人实践经验总结将各个项目进 ...
- 如何使用MySQL一个表中的字段更新另一个表中字段
[本文出自:https://www.jb51.net/article/150323.htm] 这篇文章主要介绍了如何使用MySQL一个表中的字段更新另一个表中字段,需要的朋友可以参考下 1,修改1列 ...
- Mysql跨表更新 多表update sql语句总结
Mysql跨表更新一直是大家所关心的话题,本文介绍mysql多表 update在实践中几种不同的写法 假定我们有两张表,一张表为Product表存放产品信息,其中有产品价格列Price:另外一张表是P ...
- Mysql跨表更新
Mysql跨表更新一直是大家所关心的话题,本文介绍mysql多表 update在实践中几种不同的写法,需要的朋友可以参考下 假定我们有两张表,一张表为Product表存放产品信息,其中有产品价格列Pr ...
- MySQL时间字段究竟使用INT还是DateTime
今天解析DEDECMS时发现deder的MYSQL时间字段,都是用 `senddata` ) unsigned '; 随后又在网上找到这篇文章,看来如果时间字段有参与运算,用int更好,一来检索时不用 ...
- mysql——插入、更新、删除数据(概念)
一.插入数据 1.为表的所有字段插入数据 -------------------------------------------------------------------------- (1)i ...
- 【MongoDB】递归获取字段更新表达式,更新复杂数据类型对象
在实际更新Mongo对象时发现,原有的更新代码无法更新复杂的数据类型对象.恰好看到张占岭老师有对该方法做相关的改进,因此全抄了下来. 总的核心思想就是运用反射与递归,对对象属性一层一层挖掘下去,循环创 ...
- mysql text字段判断是否为空
mysql text字段判断是否为空 mysql text字段为空select * from `tableName` where `textField` is null or `textField` ...
- 修改MySQL中字段的类型和长度
MySQL修改字段类型的命令是: mysql> alter table 表名 modify column 字段名 类型; 假设在MySQL中有一个表为:address,有一个字段为city 初始 ...
随机推荐
- 斐波那契数列 yield 和list 生成
def fab_demo4(max): a,n,b = 0,0,1 while n < max: yield b # 生成器走到这一步返回b,需要再次调用才能继续执行 a,b = b,a+b n ...
- Miller_Rabin素性测试
1. 为什么需要素性测试? 我们其实已经知道有一些判断素数的方法,比如: 遍历测试:待测试数n与2,3,...√n做除法判断余数是否为零,如果没有任何一个数可以整除n,则说明n为素数 Wilson定理 ...
- 数据类型操作简单对比(R和Python)
一.R方面 R中类型:向量(vector).数据框.矩阵.列表 数据处理转换时:数值型num.因子(factor).字符型等等 1)matrix feature:1.二维数组2.每个元素必须有相同的数 ...
- h5-背景样式
<style> div{ width: 100%; height: 150px; border: 1px solid red; /*overflow: scroll;*/ /*1.添加背景 ...
- 01 语言基础+高级:1-6 集合_day04【Map】
day04 [Map] 主要内容 Map集合 教学目标 能够说出Map集合特点 使用Map集合添加方法保存数据 使用”键找值”的方式遍历Map集合 使用”键值对”的方式遍历Map集合 能够使用Hash ...
- [mark]C# 异常处理
https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/programming-guide/exceptions/index
- 洛谷 P3371 【模板】单源最短路径(弱化版)(dijkstra邻接链表)
题目传送门 解题思路: 传送门 AC代码: #include<iostream> #include<cstdio> #include<cstring> using ...
- 格式化输入 \_\_format\_\_
格式化输入 __format__ 格式化输入 一.__format__ 自定制格式化字符串 date_dic = { 'ymd': '{0.year}:{0.month}:{0.day}', 'dmy ...
- VMware12 + Ubuntu16.04 虚拟磁盘扩容
转载自:https://blog.csdn.net/Timsley/article/details/50742755 今天用虚拟机的时候,发现虚拟机快满了,提示磁盘空间小,不得不扩充虚拟机空间.经过百 ...
- Mac 终端实现快速定位命令 自动补全目录
基于macOS oh-my-zsh 切换终端主题 incr.zsh 实现快速定位命令 自动补全目录 效果预览 步骤 1.安装 oh-my-zsh sh -c "$(curl -fsSL ht ...