如何在 pivot table 字段中 (SUM) 并在 yajra-laravel-datatable 包中搜索该字段 (laravel 5.6)

How can (SUM) in pivot table field and searching that field in yajra-laravel-datatable package (laravel 5.6)

!有三个 table

  1. 库存 enter image description here
  2. 仓库 enter image description here
  3. inventory_has_warehouses enter image description here

我用过laravel yajra datatable。我需要在 inventory_has_warehouses pivot table

中求和并搜索 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);

但我发现了这种类型的错误

Exception Message:↵↵SQLSTATE[HY000]: General error: 1111 Invalid use of group function (SQL: select count() as aggregate from (select inventories., SUM(inventory_has_warehouses.starting_balance) as total from inventories left join inventory_has_warehouses on inventory_has_warehouses.inventory_id = inventories.id left join warehouses on warehouses.id = inventory_has_warehouses.warehouse_id where inventories.subscriber_id = 2 and inventories.status = 1 and (LOWER(inventories.itemcode) LIKE %1% or LOWER(inventories.purchasedescription) LIKE %1% or exists (select * from contacts where inventories.supplier = contacts.id and LOWER(contacts.name) LIKE %1%) or (sum(inventory_has_warehouses.starting_balance) like %1%)) group by inventories.id) count_row_table)

mysql查询

select inventories., SUM(inventory_has_warehouses.starting_balance) as total from inventories left join inventory_has_warehouses on inventory_has_warehouses.inventory_id = inventories.id left join warehouses on warehouses.id = inventory_has_warehouses.warehouse_id where inventories.subscriber_id = 2 and inventories.status = 1 and (LOWER(inventories.itemcode) LIKE %1% or LOWER(inventories.purchasedescription) LIKE %1% or exists (select * from contacts where inventories.supplier = contacts.id and LOWER(contacts.name) LIKE %1%) or (sum(inventory_has_warehouses.starting_balance) like %1%)) group by inventories.id

尝试按 inventory_has_warehouses.inventory_id

分组

编辑后的答案:

您真的很难让别人阅读您的查询并为您提供帮助,但要在此处更正您的查询,请重新格式化查询并进行更正:

SELECT 
inventories.*, 
SUM(inventory_has_warehouses.starting_balance) as total 
FROM
inventories LEFT JOIN inventory_has_warehouses 
ON 
inventory_has_warehouses.inventory_id = inventories.id LEFT JOIN warehouses 
ON
warehouses.id = inventory_has_warehouses.warehouse_id 
WHERE
inventories.subscriber_id = 2 
AND
inventories.status = 1 
AND
(LOWER(inventories.itemcode) LIKE '%1%' or LOWER(inventories.purchasedescription) 
LIKE '%1%' OR EXISTS 
(SELECT 
* 
FROM
contacts 
WHERE
inventories.supplier = contacts.id 
AND
LOWER(contacts.name) LIKE '%1%') 
OR
(SUM(inventory_has_warehouses.starting_balance) LIKE '%1%')) GROUP BY inventories.id

您发送的查询的问题是您的 like 语句只有这个:

LIKE %1%

这条语句需要字符串,它只说:

inventories.

这应该特定于您需要的列或仅使用 inventory.* 来显示该列的所有列 table 但错误仍然没有意义,因为它说 and:

select count() as aggregate

也许其中之一可以解决,但在重新格式化代码后,我首先注意到语法错误,但这是非常基本的,也许你可以从 运行 一个非常简单的查询开始,目前这可能是查询这对你有用:

SELECT 
    inventories.id AS inventory_id,
    warehouses.id,
    SUM(inventory_has_warehouses.starting_balance) AS total
    FROM
    inventories LEFT JOIN inventory_has_warehouses
    ON inventories.id = inventory_has_warehouses.inventory_id
    LEFT JOIN
    warehouses 
    ON warehouses.id = inventory_has_warehouses.warehouse_id
    GROUP BY
    inventory_has_warehouses.inventory_id

从这里开始一个一个地添加条件,直到错误再次出现(在 mysql 查询 window 上执行此操作,而不是通过 laravel 代码)还不确定如何 laravel 处理 sql 查询,但您发送的格式确实会导致错误,而且如果您要 post 在这里提问,请务必使其 reader 友好,否则有人可能会抨击您很难阅读格式不正确的代码。 ;)

还有一件事我忘了确保 inventories.id 是那个 table 的主键,否则这仍然会导致您出错,请参阅此 link 了解更多详情 https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

$id = Auth::user()->id;
        $row = DB::table('inventories')->select('inventories.*','contacts.name',DB::raw('SUM(inventory_has_warehouses.starting_balance) as total'))
            ->leftJoin('contacts', 'inventories.supplier', '=', 'contacts.id')
            ->leftJoin('inventory_has_warehouses', 'inventories.id', '=', 'inventory_has_warehouses.inventory_id')
            ->where('inventories.subscriber_id',$id)
            ->groupBy('inventory_has_warehouses.inventory_id');

if ($keyword = $request->get('search')['value']) {
            $row->having(DB::raw('SUM(inventory_has_warehouses.starting_balance)'), 'like', '%'.$keyword.'%');
            $row->orHaving('inventories.itemcode', 'like', '%'.$keyword.'%');
            $row->orHaving('inventories.purchasedescription', 'like', '%'.$keyword.'%');
            $row->orHaving('contacts.name', 'like', '%'.$keyword.'%');
          }

$datatable = DataTables::of($row)

->filterColumn('total', function($query, $keyword) {
        })

return $datatable->make(true);