doujiao7520 2017-03-01 19:54
浏览 45
已采纳

从随机PHP整数填充SQL中的列的所有行

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"
  • 写回答

2条回答 默认 最新

  • duanli12176 2017-03-01 19:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探