zoukankan      html  css  js  c++  java
  • 【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
  • 相关阅读:
    js 中读取JSON的方法探讨
    git 教程 git.oschina.net
    JS实现页面跳转重定向的几种方式
    AngularJs 学习
    firebug js版
    emmt html生成
    idea sass scss配置
    关于idea激活
    sublime text There are no packages 解决!
    js监听滚动条事件
  • 原文地址:https://www.cnblogs.com/zhangdashao/p/5805664.html
Copyright © 2011-2022 走看看