You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将电商热销商品SQL查询转换为Laravel Eloquent时遇错误求助

解决Eloquent实现热销商品筛选的问题

原始SQL查询(筛选热销商品):

SELECT 
   sum(total_price), product_code, product_title, count(quantity) 
FROM 
   baskets, orders 
WHERE 
   baskets.order_id = orders.id 
   AND orders.cmd_valide = 1 
GROUP BY 
   product_code 
ORDER BY 
   sum(total_price) 
DESC LIMIT 3;

当前编写的Eloquent代码:

$basket = Basket::join('orders', 'baskets.order_id', '=', 'orders.id')
                    ->select('product_code')
                    ->get();

问题分析

你的代码存在以下几个核心问题:

  • 未添加orders.cmd_valide = 1的订单有效性筛选条件
  • 未包含原始SQL中需要的聚合字段(sum(total_price)、count(quantity))和product_title字段
  • 缺失分组、排序的逻辑实现
  • 未考虑MySQL严格模式下,GROUP BY需包含所有非聚合字段的要求

正确的Eloquent实现

$hotProducts = Basket::join('orders', 'baskets.order_id', '=', 'orders.id')
    ->where('orders.cmd_valide', 1)
    ->select(
        'product_code',
        'product_title',
        \DB::raw('sum(total_price) as total_sales'),
        \DB::raw('count(quantity) as sales_count')
    )
    ->groupBy('product_code', 'product_title') // 严格模式下必须包含所有非聚合字段
    ->orderBy('total_sales', 'desc')
    ->limit(3)
    ->get();

关键说明

  1. 筛选条件:->where('orders.cmd_valide', 1)对应原始SQL里的订单有效性判断
  2. 聚合字段处理:用DB::raw()包裹聚合函数,同时给结果起别名,方便后续调用
  3. 分组逻辑:MySQL严格模式下,GROUP BY必须包含SELECT中所有非聚合字段,因此需同时分组product_code和product_title
  4. 排序与分页:orderBy('total_sales', 'desc')和limit(3)对应原始SQL的排序和限制条数逻辑

基于模型关联的优雅实现

如果你的Basket模型已定义关联关系:

// Basket.php
public function order()
{
    return $this->belongsTo(Order::class);
}

可以用关联查询替代join,代码更符合Eloquent设计:

$hotProducts = Basket::whereHas('order', function ($query) {
        $query->where('cmd_valide', 1);
    })
    ->select(
        'product_code',
        'product_title',
        \DB::raw('sum(total_price) as total_sales'),
        \DB::raw('count(quantity) as sales_count')
    )
    ->groupBy('product_code', 'product_title')
    ->orderBy('total_sales', 'desc')
    ->limit(3)
    ->get();

内容的提问来源于stack exchange,提问作者Vin Parker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 09:50:26