【问题标题】:Laravel group by month from created_at column and sum rows multiplied with price is not workingLaravel 按月从 created_at 列分组和总行乘以价格不起作用
【发布时间】:2021-07-19 17:59:36
【问题描述】:

我需要从 2 个要按月获取成本组的表中获取数据。这两张桌子是

spare_stock_history
(spare_id, amount, created_at)

base_inventory
(id, unit_price)

我需要使用股票历史的 created_at 列获取每个月的 amount*unit_price 的总和。

我写了以下查询

 StockHistory::
        select("spare_stock_history.created_at")
        ->sum("spare_stock_history.amount*base_inventory.unit_price")
        ->join("base_inventory","base_inventory.id", "=","spare_stock_history.spare_id")
        ->where(["spare_stock_history.transaction_type"=>2])
        ->orderBy('created_at')
        ->groupBy(StockHistory::raw('MONTH(spare_stock_history.created_at)'))
        ->get();

但它给了我以下 sql 错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'spare_stock_history.amount*base_inventory.unit_price' in 'field list' (SQL: select sum(`spare_stock_history`.`amount*base_inventory`.`unit_price`) as aggregate from `spare_stock_history`)

我该如何解决这个问题?提前致谢

【问题讨论】:

  • 这能回答你的问题吗? Laravel - DB group by month
  • @Abishek ,我已根据您的建议更新了问题。你能检查一下吗?

标签: mysql laravel eloquent


【解决方案1】:

使用DB:raw 处理查询生成器上的原始表达式。

   StockHistory::
select(["spare_stock_history.created_at", DB::raw("SUM(spare_stock_history.amount * base_inventory.unit_price)")])
    ->join("base_inventory", "base_inventory.id", "=", "spare_stock_history.spare_id")
    ->where("spare_stock_history.transaction_type",2)
    ->orderBy('created_at')
    ->groupBy(DB::raw('MONTH(spare_stock_history.created_at), SUM(spare_stock_history.amount * base_inventory.unit_price)'))
    ->get();

【讨论】:

  • 得到这个错误SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'sanofi.spare_stock_history.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select spare_stock_history.created_at, SUM(spare_stock_history.amount * base_inventory.unit_price) from spare_stock_history`内部连接base_inventorybase_inventory.id = spare_id4330@4@98654@9876 /跨度>
  • @MRahi,我已经更新了代码。您的 MySQL 启用了严格模式,因此您还必须将 SELECT 上的所有列都包含在 GROUP BY 上。如果您想禁用此功能,则可以通过更新您的 database.php 配置中的 strict_mode 来实现。
  • 仍然是这个错误:虽然我已经禁用了强制模式SQLSTATE[42000]: Syntax error or access violation: 1056 Can't group on 'SUM(spare_stock_history.amount * base_inventory.unit_price)' (SQL: select spare_stock_history.created_at, SUM(spare_stock_history.amount * base_inventory.unit_price) from spare_stock_history`内部连接base_inventorybase_inventory.id=@987654342 987654343@ where spare_stock_history.transaction_type = 2 group by MONTH(spare_stock_history.created_at), SUM(spare_stock_history.amount * base_inventory.unit_price) order by created_at asc)`
  • @MRahi,如果您要禁用严格模式,请按功能从您的组中删除 SUM(spare_stock_history.amount * base_inventory.unit_price)
猜你喜欢
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
相关资源
最近更新 更多