!有三张桌子
我有用laravel yajra datatable
。我需要在inventory_has_warehouses数据透视表中对starting_balance这个字段进行求和和搜索
我的代码:
$id = Auth::user()->id;
$row = Inventory::with('contact')->with('warehouse')
->select(
'inventories.*',
DB::raw('SUM(inventory_has_warehouses.starting_balance) as total')
)
->leftJoin('inventory_has_warehouses', 'inventory_has_warehouses.inventory_id', '=', 'inventories.id')
->leftJoin('warehouses', 'warehouses.id', '=', 'inventory_has_warehouses.warehouse_id')
->where('inventories.subscriber_id',$id)
->groupBy('inventories.id');
$datatable = DataTables::of($row)
->filterColumn('total', function($query, $keyword) {
$query->whereRaw('sum(inventory_has_warehouses.starting_balance) like ?', ['%'.$keyword.'%']);
})
return $datatable->make(true);
但我发现了这种类型的错误
异常消息:↵↵SQLSTATE [HY000]:一般错误:1111 无效使用组函数(SQL:选择计数()作为聚合来自(选择
inventories
。,SUM(inventory_has_warehouses.starting_balance)作为来自inventories
左连接inventory_has_warehouses
的 总和inventory_has_warehouses
。inventory_id
=inventories
。id
左连接warehouses
onwarehouses
.id
=inventory_has_warehouses
.warehouse_id
whereinventories
.subscriber_id
= 2 andinventories
.status
= 1 and (LOWER(inventories
.itemcode
) LIKE %1% or LOWER(inventories
.purchasedescription
) LIKE %1% or exists (select * fromcontacts
whereinventories
.supplier
=contacts
.id
and LOWER(contacts
.name
) LIKE %1% ) 或 (sum(inventory_has_warehouses.starting_balance) like %1%)) 分组inventories
.id
) count_row_table)
mysql查询
选择库存。,SUM(inventory_has_warehouses.starting_balance) 作为库存的总和,在inventory_has_warehouses.inventory_id = inventory.id 上加入inventory_has_warehouses,在warehouses.id =inventory_has_warehouses.warehouse_id 上离开加入仓库,其中inventories.subscriber_id = 2 和inventories.status = 1 和( LOWER(inventories.itemcode) LIKE %1% or LOWER(inventories.purchasedescription) LIKE %1% or exists (select * from contacts where inventory.supplier = contacts.id and LOWER(contacts.name) LIKE %1%) 或 ( sum(inventory_has_warehouses.starting_balance) like %1%)) group by inventory.id