嘗試在我用“as”定義的“selectRaw”物件上使用 where 子句。
但它不起作用。
$rafbul = Inventory::selectRaw('*, sum(quantity) as quantity_sum')
->join('shelves', function($q){
$q->on('shelves.id', 'inventories.shelf_id');
$q->where('type', '!=', 'stock');
})
->where('product_id', $product_id)
->where('quantity', '>', 0)
->groupBy('shelf_id')
->orderBy('inventories.created_at')
->first();
它回來了;
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'quantity_sum' in 'where clause' (SQL: select *, sum(quantity) as quantity_sum from `inventories` inner join `shelves` on `shelves`.`id` = `inventories`.`shelf_id` and `type` != stock where `product_id` = 6188 and `quantity_sum` > 0 group by `shelf_id` order by `inventories`.`created_at` asc limit 1)
這個邏輯有什么問題嗎?
uj5u.com熱心網友回復:
要按聚合列過濾,having()/havingRaw()應使用這些方法
$rafbul = Inventory::selectRaw('*, sum(quantity) as quantity_sum')
->join('shelves', function($q){
$q->on('shelves.id', 'inventories.shelf_id');
$q->where('type', '!=', 'stock');
})
->where('product_id', $product_id)
->groupBy('shelf_id')
->havingRaw('sum(quantity) > ?', [0])
->orderBy('inventories.created_at')
->first();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/317440.html
標籤:拉拉维尔
下一篇:帶模式的PHP格式字串
