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

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

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

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

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

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

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

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