Jump to content

add random data to the table


felito

Recommended Posts

hi.

 

for debug test, i need store 200 random numbers in the database

 

I obviously want to avoid insert one by one

 

how i can do that? (mysql)

 

i discover this, but is not what i pretend.

 

UPDATE tablename SET column1 = (rand() * 50)

 

Link to comment
Share on other sites

<?php
$num = 200;
$arr = array();
while(count($arr) < $num){
$rand = rand();
if(!in_array($rand,$arr)){
	$arr[] = $rand;
}
}

$data = implode('),(',$arr);
$query = "INSERT INTO your_table (your_column) values ($data)";
echo $query;

//execute your query here...

?>

Link to comment
Share on other sites

Hi

 

This will do it for you:-

 

INSERT INTO testrandom
(`aRandomNumber`)
SELECT (rand() * 50)
FROM (SELECT a.i +b.i*10 + c.i*100 AS  someinteger
FROM integers a,integers b,integers c
HAVING someinteger < 200) fred

 

You need a table called integers with an integer column called i, with 10 rows. i has values from 0 to 9 in those rows. This allows you to generate a number, and here is used to generate all the numbers between 0 and 999, with any greater than or equal 2 disposed of by the HAVING clause. This gives you 200 rows. The select then creates a random number for each row and that is inserted into the column named in the INSERT query (this is assuming an autonumber primary key).

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.