mysql批量补充uuid, Function UUID() and REPLACE(), can't work together

1.表设计

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `uuid` varchar(32) COLLATE utf8_bin DEFAULT '' COMMENT 'uuid',
  `name` varchar(255) COLLATE utf8_bin DEFAULT '' COMMENT '用户名',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

 

2.补全uuid

update user u
inner join (select id, replace(uuid(),"-","") uuid from user ) r on r.id = u.id
SET u.uuid = r.uuid
where u.uuid = '' or u.uuid is null

 

3.注意问题

当mysql字符集编码为uftmp4,查询replace(uuid(),"-","")一致

mysql批量补充uuid, Function UUID() and REPLACE(), can't work together_第1张图片

mysql批量补充uuid, Function UUID() and REPLACE(), can't work together_第2张图片

方案1:临时设置字符集编码 set names utf8

mysql批量补充uuid, Function UUID() and REPLACE(), can't work together_第3张图片

mysql批量补充uuid, Function UUID() and REPLACE(), can't work together_第4张图片

方案二:replace(convert(uuid() using utf8mb4), '-','')

mysql批量补充uuid, Function UUID() and REPLACE(), can't work together_第5张图片

解决问题参考:https://bugs.mysql.com/bug.php?id=12774

你可能感兴趣的:(DataBases)