如何在pivot表字段中(sum)并在yajra laravel datatable包(laravel5.6)中搜索该字段

ymzxtsji  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(322)

! 有三张table
清单在此处输入图像描述
仓库管理员图片说明
这里有库存仓库的图像描述
我有用处 laravel yajra datatable . 我需要在库存数据透视表中的开始余额字段的总和和搜索
我的代码:

$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:select count()as aggregate from(select) inventories ,sum(库存有仓库,起始余额)作为 inventories 左连接 inventory_has_warehousesinventory_has_warehouses . inventory_id = inventories . id 左连接 warehouseswarehouses . id = inventory_has_warehouses . warehouse_id 哪里 inventories . subscriber_id =2和 inventories . status =1和(下)( inventories . itemcode )如%1%或更低( inventories . purchasedescription )与%1%相似或存在(从中选择* contacts 哪里 inventories . supplier = contacts . id 和更低( contacts . name )如%1%)或(sum(inventory\u has \u warehouses.starting\u balance)如%1%))group by inventories . id )计数(行表)
mysql查询
选择库存。,sum(inventory\u has \u warehouses.starting \u balance)作为来自库存left join inventory\u has \u warehouses on inventory\u has \u warehouses.inventory\u id=inventory.id left join warehouses on warehouses.id=inventory的合计,其中inventory.subscriber\u id=2,inventory.status=1和(较低的(inventory.itemcode),如%1%或更低(inventory.purchasedescription)如%1%或存在(select*from contacts where inventory.supplier=contacts.id and lower(contacts.name)如%1%)或(sum(inventory\u has\u warehouses.start\u balance)如%1%)按inventory.id分组

vkc1a9a2

vkc1a9a21#

尝试按库存进行分组\u有\u仓库。库存\u 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.*来显示该表的所有列,但该错误仍然没有意义,因为它表示和:

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查询窗口上执行此操作,而不是通过laravel代码)还不确定laravel如何处理sql查询,但您发送的格式确实会导致错误,而且如果您要在此处发布问题,请确保它对读者友好,否则可能会有人抨击您造成的错误格式不正确的代码很难读懂。;)
还有一件事我忘了,请确保inventory.id是该表的主键,否则仍会导致错误。有关详细信息,请参阅此链接https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

7fhtutme

7fhtutme2#

$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);

相关问题