1

我的数据库是这样的

id |  product_id |   cost1 |   cost2
1       2          33      20
2       2          25      36
3       3          10      15

我需要 product_id 2 的最小成本(cost1 和 cost2 的最小值)的结果,我正在使用 laravel 框架。有人帮我吗?

4

1 回答 1

0

您可以从多个字段中获取最小值,如下所示:

$query = DB::table('product')
    // to get multiple min values
    ->selectRaw('min(cost1) AS minCost1, min(cost2) AS minCost2') 
    ->where('product_id', 2);

要从最小值中获取最小值,请尝试以下操作:

$query = DB::table('product')    
    // get least value from min of two fields
    ->selectRaw(' LEAST(min(cost1), min(cost2)) AS finalMin') 
    ->where('product_id', 2);

LEAST()将返回最小的元素

于 2018-02-23T09:55:20.920 回答