![](/img/trans.png)
[英]How to get the max and min of a data from child table with mysql and php?
[英]How to get max and min of mysql column from another related table in Laravel
这是我的数据库表
现在每个请求都有一个产品,每个列表都有许多请求,我现在想获取列表X中产品的最小和最大价格/尺寸,如何使用Laravel Elequent实现呢?
列表表
+----+----------+
| id | user_id |
+====+==========+
| 1 | 1 |
+----+----------+
| 2 | 1 |
+----+----------+
| 3 | 2 |
+----+----------+
要求表
+----+-------------+----------+
| id | product_id | list_id |
+====+=============+==========+
| 1 | 1 | 1 |
+----+-------------+----------+
| 2 | 1 | 1 |
+----+-------------+----------+
| 3 | 2 | 2 |
+----+-------------+----------+
产品表
+----+-------+------+-------+
| id | price | size | other |
+====+=======+======+=======+
| 1 | 110 | 10 | test |
+----+-------+------+-------+
| 2 | 130 | 12 | test |
+----+-------+------+-------+
| 3 | 100 | 24 | test |
+----+-------+------+-------+
| 4 | 200 | 16 | test |
+----+-------+------+-------+
我想获取清单X中产品的最小和最大价格/尺寸
$maxSize = Product::whereHas('request.list', function($q) use($listId) {
$q->where('list.id', $listId);
})
->max('size');
$minSize = Product::whereHas('request.list', function($q) use($listId) {
$q->where('list.id', $listId);
})
->min('size');
对最低和最高价格执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.