【发布时间】:2021-11-09 07:59:15
【问题描述】:
我有两个表“inventory”和“sell_records”的关系。库存中的每个产品都有许多销售记录。 我需要按最新销售记录订购所有产品。所以,我需要从产品的所有销售记录中找出该产品的最新销售记录,然后对所有产品进行排序。
这是我想出的脚本,但它仍然没有按应有的方式排序。
$inventory = Inventory::where('inventory.client_id', $user->client_id)
->join('inventory_sell_records', 'inventory_sell_records.product_id', '=', 'inventory.id')
->groupBy('inventory_sell_records.product_id')
->orderByRaw("max(inventory_sell_records.created_at) $order_by")
->paginate(100);
【问题讨论】:
标签: php mysql laravel eloquent relational-database