【问题标题】:How can (SUM) in pivot table field and searching that field in yajra-laravel-datatable package (laravel 5.6)如何(SUM)在数据透视表字段中并在 yajra-laravel-datatable 包中搜索该字段(laravel 5.6)
【发布时间】:2018-04-27 07:24:22
【问题描述】:

!三张桌子

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

我用过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: select count() as aggregate from (select inventories.,SUM(inventory_has_warehouses.starting_balance) 为 总计从inventories 左加入inventory_has_warehouses on inventory_has_warehouses.inventory_id = inventories.id 左 加入warehouseswarehouses.id = inventory_has_warehouses.warehouse_id 在哪里 inventories.subscriber_id = 2 和 inventories.status = 1 和 (LOWER(inventories.itemcode) 喜欢 %1% 或 LOWER(inventories.purchasedescription) LIKE %1% 或存在(选择 * 来自contacts 其中inventories.supplier = contacts.id 和 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 on inventory_has_warehouses.inventory_id = inventory.id 离开加入 仓库上的仓库.id = inventory_has_warehouses.warehouse_id 其中 inventory.subscriber_id = 2 和 inventory.status = 1 和 (LOWER(inventories.itemcode) LIKE %1% 或 LOWER(inventories.purchasedescription) LIKE %1% 或存在(选择 * 来自inventories.supplier = contacts.id的联系人和 LOWER(contacts.name) LIKE %1%) 或 (sum(inventory_has_warehouses.starting_balance) like %1%)) 分组 库存.id

【问题讨论】:

标签: mysql laravel-5.6 php-7.1 yajra-datatable


【解决方案1】:
$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);

【讨论】:

    【解决方案2】:

    尝试按 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.
    

    这应该特定于您需要的列,或者只使用库存。* 来显示该表的所有列,但错误仍然没有意义,因为它说和:

    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查询,但您发送的格式确实会导致错误,如果您要在此处发布问题,请确保使其易于阅读,否则有人可能会抨击您,导致难以阅读格式不正确的代码。 ;)

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

    【讨论】:

    • 尝试先从您的查询中删除库存。*,然后尝试运行,如果可行的话将库存放在。*在 sum 函数更改查询顺序之后。
    • 数据获取成功,但我在数据表中搜索而不是给出错误“无效使用组函数”
    • 您的联接声明中不包含库存表。
    • 对不起,我不理解
    • 尝试输出运行的原始查询并将其发送到此处。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    相关资源
    最近更新 更多