I have a SQL database with data however would like to populate a certain field in every row with a different random integer.
The code works to populate the field with one single random integer therefore my question is how do you loop for each row while assigning it a random integer.
Edit: Populating it dependent on another field condition
PHP Code Snippet:
$data = rand(intval(5000),intval(57000));
echo $data;
$sql = "UPDATE Test SET Projection = ".$data;
mysqli_query($conn,$sql);
echo "complete"
you're about to update
table with random integer, use MySQL:
SELECT (FLOOR( 1 + RAND( ) * 100 )) -- returns random int in 1-100
your php code:
//$data = rand(intval(5000),intval(57000));
//echo $data;
$low = 5000 ; $high = 57000 ; $d = $high - $low ;
$sql = "UPDATE Test
SET Projection = (FLOOR( $low +RAND()* $d ))
WHERE DayColumn in ( 'Friday' , 'Saturday' , 'Sunday')
;" ;
mysqli_query($conn,$sql);
echo "complete"
If you want do some thing else based on this value, before updating or after, let me know to edit my answer.
You could use an update too eg:
Update Test set Projection = FLOOR(1 + RAND() * 57000 )
To obtain a random integer R in the range i <= R < j,
use the expression
FLOOR(i + RAND() * (j − i))
see man https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand