【发布时间】: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 ,我已根据您的建议更新了问题。你能检查一下吗?