MYSQL insert random from list

后端 未结 2 1063
终归单人心
终归单人心 2020-12-01 07:36

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 l

相关标签:
2条回答
  • 2020-12-01 08:16
    INSERT INTO `im`
    (`im`, `service`)
    SELECT LOWER(`last`),
        ELT(0.5 + RAND() * 6, 'AIM', 'ICQ', 'MSN', 'Yahoo', 'GTalk', 'Other')
    FROM `contact`
    
    0 讨论(0)
  • 2020-12-01 08:27

    If you're willing to add the services to another table...

    INSERT INTO `im` ( `im` , `service` )
    SELECT LOWER( `last` ) , (
       SELECT `service`
       FROM `service`
       ORDER BY RAND( )
       LIMIT 1
    )
    FROM `contact`
    
    0 讨论(0)
提交回复
热议问题