MYSQL从列表中随机插入 [英] MYSQL insert random from list
问题描述
我想向表中添加一个随机值.虽然我知道如何在一个范围内添加随机整数,但目前我对如何从列表中添加随机选择的项目感到困惑.
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.
比方说,我有一个用于IM帐户的MYSQL表.我想用随机数据填充它.
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`;
此查询应该执行的操作是将联系人的姓氏添加到IM表中,并从我要提供的数组中随机选择.例如,该数组为:
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.
注意:我知道使用PHP,Perl等编程语言完全可以做到这一点,但是我没有这样做.请尝试提供一种使用MYSQL严格执行此操作的方法.
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.
推荐答案
INSERT INTO `im`
(`im`, `service`)
SELECT LOWER(`last`),
ELT(0.5 + RAND() * 6, 'AIM', 'ICQ', 'MSN', 'Yahoo', 'GTalk', 'Other')
FROM `contact`
这篇关于MYSQL从列表中随机插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!