laravel eloquent 从同一列的两个字段中获取最小值
laravel eloquent get min value from two field in same column
我的数据库是这样的
id | product_id | cost1 | cost2
1 2 33 20
2 2 25 36
3 3 10 15
我需要 product_id 2 的最小成本(成本 1 和成本 2 的最小值)的结果
我正在使用 laravel 框架。
有人帮帮我吗?
您可以像这样从多个字段中获取最小值:
$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()将return最小的元素
我的数据库是这样的
id | product_id | cost1 | cost2
1 2 33 20
2 2 25 36
3 3 10 15
我需要 product_id 2 的最小成本(成本 1 和成本 2 的最小值)的结果 我正在使用 laravel 框架。 有人帮帮我吗?
您可以像这样从多个字段中获取最小值:
$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()将return最小的元素