在MYSQL中将随机数插入表中 [英] Inserting random numbers into a table in MYSQL
本文介绍了在MYSQL中将随机数插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图计算表中的行数,并为字段random生成随机数
现在这个工作:
Im trying to count the number of rows in the table and generate random numbers for the field 'random' Now this works:
SELECT COUNT(*) FROM my_table;
并可运作:
UPDATE my_table SET random = FLOOR(6500 * RAND()) + 1;
但这不起作用:
UPDATE my_table SET random = FLOOR((SELECT COUNT(*) ) * RAND()) + 1;
但是这会将行计数为0,并添加一个,因此所有字段都具有数字,随机数。
But this counts the rows as 0 and adds one so all fields have the number one instead of a unique random number.
任何想法,我做错了都是最有帮助的。
Any ideas what I'm doing wrong would be most helpful.
推荐答案
这是什么?
SELECT @cnt := count(*) FROM my_table;
UPDATE my_table SET random = FLOOR(@cnt * RAND()) + 1;
演示: http://sqlfiddle.com/#!2/a896d/4
这篇关于在MYSQL中将随机数插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文