php - 如何组合这 2 个 SQL 查询?
问题描述
我正在尝试组合来自多个表的数据并将其发送到刀片模板。
我试图把total_price
with ->select('products.name', total_price
,但我得到了
语法错误或访问冲突:1055 SELECT 列表的表达式 #2 不在 GROUP BY 子句中,并且包含非聚合列`
$orders = DB::table('orders')
->join('order_items', 'order_items.order_id', '=', 'orders.id')
->join('customers', 'customers.id', '=', 'orders.customer_id')
->join('products', 'products.id', '=', 'order_items.product_id')
->select('products.name', DB::raw('SUM(order_items.quantity * order_items.unit_price) as total, SUM(order_items.quantity) as quantity'))
->whereYear('orders.created_at', $currentYear)
->whereMonth('orders.created_at', $currentMonth)
->where('orders.customer_id', $id)
->groupBy('order_items.product_id');
$order_prices = DB::table('orders')->select('total_price')->get();
解决方案
推荐阅读
- php - 在 php 7.3 Mac OS Mojave 上启用 apcu
- javascript - 根据值在输出之前对 JSON 成员进行排序和 for 循环
- jenkins - Jenkins 共享库中未调用构造函数
- c# - 如何在 c#.net 中将对象从表单访问到另一个类
- ios - 更新材质,然后更改对象的位置。如何解决?
- sql-server - 当我尝试构建数据库图时,SQL Server Management Studio 崩溃
- .net - 有没有办法将字符串从 angular 7 传递到 .Net Core 2.0 API?
- r - 在实例化对象运行期间更改属性值
- c# - IFormFile 作为 Web API 中的类的属性
- .net - .NET 框架 4.0 的 Visual Studio 单元测试