如果您想为此字段创建独特的值,您可以使用自增长方法,只要是基于36进制即可。下面是一个例子,可以生成数亿个不同的值:
update t cross join (select @i := 0, @chars = '0123456789abcdefghijklmnopqrstuvwxyz') const
set hash = concat(substring(@chars, ((@i := @i + 1) %36)+1, 1),
substring(@chars, floor(@i/pow(36, 1))%36 + 1, 1),
substring(@chars, floor(@i/pow(36, 2))%36 + 1, 1),
substring(@chars, floor(@i/pow(36, 3))%36 + 1, 1),
substring(@chars, floor(@i/pow(36, 4))%36 + 1, 1),
substring(@chars, floor(@i/pow(36, 5))%36 + 1, 1),
'0000'
);
编辑:(基于修改后的问题)
你的表上有一个唯一约束条件。我会采取以下措施:
insert into vouchers(hash)
select concat(substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1),
substring(@chars, floor(rand()*36) + 1, 1)
);
只需在循环中重复执行此操作(或根据需要执行),即可填充表格。极不可能出现重复的情况,如果出现,则该插入操作将失败。