舍入laravel查询的结果

时间:2019-02-15 10:55:36

标签: laravel

大家好,我有一个简单的问题,我正在对selectRaw查询进行一些数学运算,但是我需要对结果进行四舍五入(例如使用floor())。如何在我的selectRaw查询上做到这一点?

            $primer =  DB::connection('lab_inv')
          ->table('products')
          ->join('primer_adaptor', 'products.id', 'primer_adaptor.Product_id')
          ->selectraw('primer_adaptor.Product_id, primer_adaptor.Primer_Name,
    primer_adaptor.Amount / primer_adaptor.Amount_Run AS Amount,
    products.Amount / (products.Amount_Run * products.Runs_week) AS Runs, (products.Runs / products.Runs_week) * 5 AS Running_days')
          ->get();

1 个答案:

答案 0 :(得分:1)

您可能只使用MySQL的FLOOR函数,就像您在原始MySQL查询中通常使用的那样,例如

$sql = "pa.Product_id, pa.Primer_Name, pa.Amount / pa.Amount_Run AS Amount, ";
$sql .= "FLOOR(p.Amount / (p.Amount_Run * p.Runs_week)) AS Runs, ";
$sql .= "(products.Runs / products.Runs_week) * 5 AS Running_days";
$primer =  DB::connection('lab_inv')
    ->table('products AS p')
    ->join('primer_adaptor AS pa', 'p.id', 'pa.Product_id')
    ->selectraw($sql)
    ->get();

在这里,我只是为您命名为“ Runs”的别名,但是您可以在任何需要的地方使用FLOOR