I would like to add a random value to a table. While I know how to add random integers within a range, I am currently stumped on how to add a randomly selected item from a list.
Let's say I have a MYSQL table for IM accounts. I would like to fill it with random data.
INSERT INTO `im` (`im`, `service`) SELECT LOWER(`last`), RANDOM-SELECTION-HERE FROM `contact`;
What this query should do is add to the IM table the contact's last name with a random selection from an array I would give. For example, the array would be:
['AIM', 'ICQ', 'MSN', 'Yahoo', 'GTalk', 'Other']
So, I would like to add the users last name plus one of the random items from the array.
NOTE: I know this is fully possible with a programming language such as PHP, Perl, etc., but I am not trying to do that. Please try to provide a way to do this strictly with MYSQL.
select FLOOR( RAND() * (maximumValue-minimumValue) + minimumValue) as anyVariableName; Let us check with some maximum and minimum value. The maximum value we are considering is 200 and minimum is 100. The random number will be between 100 and 200 including 100 and 200 itself.
UPDATE yourTableName set yourColumnName=value where yourColumnName2=(SELECT FLOOR(1+RAND()*3)); In the above query, the statement FLOOR(1+RAND()*3) generates the number between 1-3 and update the column.
INSERT INTO `im` (`im`, `service`) SELECT LOWER(`last`), ELT(0.5 + RAND() * 6, 'AIM', 'ICQ', 'MSN', 'Yahoo', 'GTalk', 'Other') FROM `contact`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With