【问题标题】:Create many rows in Mysql with php random number generator使用 php 随机数生成器在 Mysql 中创建多行
【发布时间】:2015-10-29 13:57:40
【问题描述】:

我需要在我的 mysql 数据库中创建超过 1000 行。我编写了一些代码,它生成一个随机数,其中包含 5 个整数,数字在 2-8 之间。

我想用生成的数字在我的数据库中创建一个新行。

我有这一切,但我不知道让代码记住已经插入的值。

有没有办法做到这一点,而不必将所有值存储在一个数组中,然后在插入新行之前让代码检查孔数组?

有没有更聪明的方法?

我的代码:

$length = 5;

echo '"';
for ($i = 0; $i < $length; $i++) {

    echo generateRandomString('3'). '", "';

}


function generateRandomString($length = 10) {
    $characters = '2345678';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
echo '"';
?>

【问题讨论】:

  • 如果你关心之前的随机性,我不确定我是否明白。为什么不完全在没有 PHP 的情况下在 sql 中完成它(除了说 DO IT 的调用)
  • @DrewPierce 的建议很好,我认为你必须考虑一下,按照建议的方式你会赢得很多性能。
  • 我们又见面了@maytham!
  • 而且我一直很喜欢你的解决方案 ;) 顶一下

标签: php mysql sql-insert bulkinsert


【解决方案1】:

这只是一个简单的例子,有点矫枉过正,你可能需要在随机部分有 1 行。但这就是我目前所拥有的一切,必须出去。

create schema so_gibberish; -- creates database 
use so_gibberish;   -- use it 

-- drop table random;   -- during debug
create table random 
(   id int auto_increment primary key,
    question varchar(50) not null,
    category int not null,
    randomOrder int not null,
    key (category)
);

创建一个存储过程以插入随机问题(以 ? 结尾) 调用时一次创建 300 个

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DELIMITER $$ 
drop procedure if exists createRandomQuestions$$ 
-- 17 categories of questions randomly created. yes random word questions and categories.

create procedure createRandomQuestions()
BEGIN
set @i=1;
WHILE @i<=300 DO
insert random (question,category) values ('xxx',1);
SELECT @lid:=LAST_INSERT_ID();  -- use id to seed, next 8 guaranteed different i think

-- programmer should come up with adequate random seed on their own
-- this is WAY overkill but it was what I had at the moment to show you

UPDATE random SET question=concat(
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@lid)*4294967296))*36+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed)*36+1, 1), ' ?'
), category=floor(rand()*17+1),randomOrder=0
WHERE id=@lid;
set @i=@i+1;
END WHILE;
END;
$$
DELIMITER ;
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

叫它:

call createRandomQuestions();

清理:

drop schema so_gibberish;

祝你好运。

【讨论】:

    【解决方案2】:

    您可以创建一个包含您之前插入的所有数字的数组,并将您当前的数字与数组中的内容进行比较:

    $inserted_numbers = array()
    if( in_array( $number, $inserted_numbers ) ) {
         // The numbers is already in the array 
    } else {
         // NUmber not in array, do stuff
         array_push( $inserted_numbers, $number )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      相关资源
      最近更新 更多