duanjiong2021 2013-11-20 06:11
浏览 326

创建条目时,在列中生成随机且唯一的ID

How can I make it so when an entry is created in a table, one of the columns unique_id automatically gets filled with a randomly generated integer? Can this be done straight from mysql? I've seen that you can automatically fill it with a timestamp and such, so is it possible to automatically fill it with a randomly generated integer?

  • 写回答

4条回答

  • dongyuan2388 2013-11-20 06:15
    关注

    you can try this

    SELECT FLOOR(10000 + RAND() * 89999) AS my_random_number
    FROM mytable
    WHERE "my_random_number" NOT IN (SELECT id FROM mytable)
    LIMIT 1
    

    OR SImply this SELECT FLOOR(RAND() * 1000000);

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题