MySQL-使用最后12位数字的随机值更新联接的列

时间:2018-09-17 19:20:57

标签: mysql

我有一个名为Joined(varchar(50))的MYSQL列,其值例如13个数字 如何实现该查询?

BufferedReader reader= new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
// Waiting
 builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();

2 个答案:

答案 0 :(得分:2)

您可以生成md()随机字符串,例如:

UPDATE `users` 
SET `joined` = concat('153',SUBSTRING(MD5(RAND()) FROM 1 FOR 10) )

UPDATE `users` 
SET `joined` = concat('153',SUBSTRING(MD5(RAND()) FROM 1 FOR 6), 
                            SUBSTRING(MD5(RAND()) FROM 1 FOR 4) )

或者如果只需要数字就可以使用

UPDATE `users` 
SET `joined` = concat('153',CAST(rand()*1000000 as UNSIGNED), 
                            CAST(rand()*10000 as UNSIGNED) )

of对于带有短字符串的行,您可以在根据需要调整代码时使用

UPDATE `users` 
SET `joined` = case when length(joined)= 14 then
                concat('153',CAST(rand()*10000000 as UNSIGNED), 
                            CAST(rand()*10000 as UNSIGNED) )
                    when  length(joined)= 13 then
                concat('153',CAST(rand()*1000000 as UNSIGNED), 
                            CAST(rand()*10000 as UNSIGNED) )
                .....
                end 

答案 1 :(得分:2)

尝试:

UPDATE `users` 
SET `joined` = CONCAT('153', FLOOR(RAND()* 9000000000) + 1000000000)