【mysql】关于循环插入数据 存储设计
要求插入的数据有一定的规律
1 新建实例列表 2 CREATE TABLE users 3 ( 4 userId INT(11) NOT NULL, 5 userName VARCHAR(255) NOT NULL, 6 Serves INT(11) NOT NULL, 7 PRIMARY KEY (userId) 8 ); 9 10 创建存储过程 例如:随机写入5000条数据 11 12 begin 13 declare i int default 1; 14 start transaction; 15 while i<=5000 do 16 insert into users(userId,userName,Serves) values(i,concat('z00',i),123); 17 set i=i+1; 18 end while; 19 commit; 20 end
设置随机函数取前几条 select * from users order by rand() LIMIT 3 ;
users表刚才插入5000条数据 SELECT * FROM users WHERE userId >= ( (SELECT MAX(userId) FROM users WHERE userId >= '10')- (SELECT MIN(userId) FROM users WHERE userId<= '22') ) * RAND() + (SELECT MIN(userId) FROM users WHERE userId <= 65) LIMIT 5 那么上面的随机取五条可以换算成 SELECT * FROM users WHERE userId >= (5000-1)*rand()+1 limit 5