我的数据库是这样的
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()将返回最小的元素
关于php - laravel Eloquent 地从同一列的两个字段中获取最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48944998/