dqlm80253 2015-07-22 01:08
浏览 22
已采纳

根据其他列值计算列值

I need to calculate a columns value based on another columns value from the same table and can't find a how to in the documentation. My code is as follows:

$toChange = json_decode($request->toSend);
        $database = $request->user()->id + 1;
        Config::set('database.connections.usertable.database','usertable' . $database );
        $insert = new Products();
        foreach ($request->countries as $key => $value) {
            if ($value === true) {
                foreach ($toChange as $change) {
                    $insert->setTable($key . '_products');
                    $insert->whereBetween('price',[$change->priceRangeFrom,$change->priceRangeTo])->update([
                        'ip_address' => $request->getClientIp(),
                        'opt_active' => 1,
                        // next line is the problem
                        'price' => 'price' / 100 * (100 - $change->minPrice) 
                    ]);

                }

            }
        }

I update a few thousand entries with that and need to get the value from the price field from every column and calculate the new price from it. Can someone advise how to do so?

The query should be like this pdo query

$stmt = $pdo->prepare("UPDATE uk_products SET min_price = (price * ((100 - :min) / 100)) /* ... */ WHERE price BETWEEN :minrange AND :maxrange");
                $stmt->execute([':min' => $data['min'] /* ... */ ]);

where this is the calculation in question

min_price = (price * ((100 - :min) / 100)) 
  • 写回答

2条回答 默认 最新

  • dongshi8038 2015-07-22 01:50
    关注

    This will work, in case someone else needs it

    'min_price' => DB::raw('price / 100 * (100 - '.$change->minPrice.')')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据