mysql重命名已重名用户昵称

公司用的Sequel Pro 不支持create function.

本来会有个好用的随机数产生函数

delimiter $
CREATEFUNCTIONrandom_str(nint)RETURNSvarchar(255)
begin
declarechars_strvarchar(100)
default"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
declarereturn_strvarchar(255)default"";
declareiintdefault0;
while i < n do
setreturn_str=concat(return_str,substring(chars_str,floor(1+rand()*52),1));
seti= i+1;
endwhile;
returnreturn_str;
end$$
delimiter ;

然后并写不了,找到了一个好方法就是用 md5()+ right
最后sql为

update PN_UserTest as u1 inner join PN_UserTest as u2 on (u1.userName = u2.userName) and u1.id != u2.id set u1.userName = CONCAT(u1.userName, '-', right(MD5(rand()),5));

userId最小的不能更新...

select min(userId) from PN_UserTest group by hex(username) having count(*) > 1;

最后语句是:

update PN_UserTest as u1 inner join PN_UserTest as u2 on (hex(u1.userName) = hex(u2.userName)) and u1.id != u2.id set u1.userName = CONCAT(u1.userName, '-', right(MD5(rand()),5))
 where u1.id not in (select min(id) from (select * from PN_UserTest) a group by hex(a.username) having count(*) > 1);

注意!!! where in 里 要先用虚表a 先保存 PN_UserTest的最小id, 因为mysql不能先select出同一表中的某些值,再update这个表(在同一语句中) userName 也要带hex才能真正区分emoji之类的字符集, 自行搜索下mysql collocate 看下原理.
注意2 在我们只有几十万用户的数据库,用这个语句就崩了,原因是hex不能用到索引.
解决办法是SELECT group_concat(userId),username as cnt
FROM PN_User
GROUP BY hex(username)
HAVING cnt > 1
查出,然后手动插到where条件里..

更新

语句其实还是能优化的!!!!!

update PN_User u1 join (
(SELECT MIN(userId) as userId,userName
 FROM PN_User
 GROUP BY hex(userName)
 HAVING COUNT(*) > 1)
) uid
on uid.userId != u1.userId and hex(u1.userName) = hex(uid.userName) and u1.userName = uid.userName
SET u1.userName = CONCAT(u1.userName, '-', RIGHT(MD5(rand()), 5));

这里

and hex(u1.userName) = hex(uid.userName) and u1.userName = uid.userName

是为了能启用userName索引,被函数包含的字段无法启用索引.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是SQL数据库: SQL是Structured Query Language(结构化查询语言)的缩写。SQL是...
    西贝巴巴阅读 1,855评论 0 10
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,250评论 0 7
  • 1、说明:创建数据库CREATE DATABASE database-name2、说明:删除数据库drop dat...
    codeSirCao阅读 455评论 0 2
  • 上一章 柯先生跟唐三角说,搬过来吧,唐三角摇摇头说不要,柯先生笑眯眯,亲亲唐三角头发。“去给多肉浇浇水吧。” “好...
    我叫唐三角阅读 424评论 8 3
  • 今天北京天气,晴朗,风和日丽可见度极好。今年来北京玩的第三天,开始骑摩拜的第二天,哈,摩拜出来好久,第一次...
    拂晓_源阅读 328评论 2 0