PHP生成随机数并批量更新到MySQL数据库

        Debug::remark('begin');
        $sql = ' UPDATE `xzd_select`
        SET lunch1_rand = CASE id ';
        $res = db('xzd_select')->order('id', 'asc')->select();
        foreach ($res as $val) {
            $num = $val['id'].rand(1,999);
            $sql .= " WHEN " . $val['id'] . " THEN " . $num;
        }
        $sql .= " END, ";
        $sql .= " lunch2_rand = CASE id";
        foreach ($res as $val) {
            $num = $val['id'].rand(1,999);
            $sql .= " WHEN " . $val['id'] . " THEN " . $num;
        }
        $sql .= " END, ";
        $sql .= " dinner1_rand = CASE id";
        foreach ($res as $val) {
            $num = $val['id'].rand(1,999);
            $sql .= " WHEN " . $val['id'] . " THEN " . $num;
        }
        $sql .= " END ";
        $sql .= "WHERE id IN ( " . implode(', ', array_column($res, 'id')) . " )";
        Db::execute($sql);
        Debug::remark('end');

        echo '使用了'. Debug::getRangeTime('begin','end').'s';

你可能感兴趣的:(PHP生成随机数并批量更新到MySQL数据库)