前提
我这个人有点缺乏自制力,又懒散,同时记忆力衰退还有点快 QAQ。决定用这篇文章记录下最近学习的内容,方便回顾的同时,也告诉自己真的有在进步,避免对自己失去了热情。(持续更新ing~)
2022.7.17
1、MySQL 的 in 操作有数量限制吗?
答:没有。Oracle 中限制的是 1000 个,MySQL 则没有限制,但是整体 sql 长度会有限制,控制 sql 长度的参数 max_allowed_packet
默认是 4 MB。可以根据命令 show VARIABLES like '%max_allowed_packet%';
查看参数的值
官方文档 :https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_allowed_packet
衍生出比较有趣的文档:http://t.csdn.cn/BzLoW
2022.7.26
今天遇到了一个线上 db 数据的问题,业务发现数据库中 sort 排序字段存在相同的值(同一张表),要求我们将 sort 字段修复。这种问题就是写 SQL 语句去修复了,首先考虑的就是要修复成什么样子,怎么修复?决定按照之前 sort + id(自增id)升序出来的 list 顺序,将 sort 重新赋值
list 顺序也就是行号,是表里不存在的一个字段,先来看看 MySQL 如何查询出来
select @rownum := @rownum +1 as rownum,字段 from (select @rownum := 0) r, 表1 p where 条件 order by sort,id;
第二步,就是将 rownum 赋值给 sort 字段,想到以下两种 SQL 都可以
1、update 表1 a right join (select @rownum := @rownum +1 as rownum,字段 from (select @rownum := 0) r, 表1 p where 条件 order by sort,id) b on a.aid = b.aid and a.unionPriId = b.unionPriId and a.rlPdId = b.rlPdId and a.sysType = b.sysType set a.sort = b.rownum where a.sort is not null;
2、update 表1 a, (select @rownum := @rownum +1 as rownum,字段 from (select @rownum := 0) r, 表1 p where 条件 order by sort,id) b set a.sort = b.rownum where a.aid = b.aid and a.unionPriId = b.unionPriId and a.rlPdId = b.rlPdId and a.sysType = b.sysType;