【问题标题】:I found Error in query when i join with another table当我加入另一个表时,我在查询中发现错误
【发布时间】:2016-01-27 10:57:51
【问题描述】:

当我将一张桌子加入另一张桌子时,我收到了这个错误。我把它作为关键字,但它不起作用它给我错误,比如找不到字段我该如何解决这个错误

代码:

Product::leftjoin('reviews','products.id','=','reviews.productID')
                           ->select(array('products.*',
                                    DB::raw('AVG(rating) as ratings_average')
                                ))
                        ->where(function($query) use ($categoriesID,$brands,$priceArray,$ratingArray)
                            {
                                $query->whereIn('categoryID',$categoriesID);
                                if(count($brands) > 0)
                                {
                                    $query->whereIn('brandID',$brands);
                                }
                                $query->whereBetween('productSellingPrice',$priceArray);
                                if(count($ratingArray) > 0)
                                {
                                    $query->whereBetween('ratings_average',$ratingArray);
                                }
                            })
                        ->groupBy('products.id')
                        ->get();

错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ratings_average' in 'where clause' (SQL: select `products`.*, AVG(rating) as ratings_average from `products` left join `reviews` on `products`.`id` = `reviews`.`productID` where (`categoryID` in (9, 11, 31) and `productSellingPrice` between 50 and 5000 and `ratings_average` between 1 and 2) group by `products`.`id`)

【问题讨论】:

  • 你的表是否有一个名为ratings_average的列?
  • 我使用 DB::raw('AVG(rating) as ratings_average' @TZHX
  • 抱歉我听不懂你@TZHX
  • 您没有具有该名称的列,因此您不能将其包含在条件中。这是基本的 MySQL。
  • 只需检查我用作平均@TZHX 关键字的查询

标签: php mysql sql laravel model-view-controller


【解决方案1】:

您可以找到$minRatingmaxRating,而不是评级数组。通过这两个值,您可以像这样运行查询:

$query->whereBetween('rating',[$minRating,$maxRtaing]);

【讨论】:

  • 使用这个$query->whereBetween('AVG(`rating`)',$ratingArray); 不确定您是否需要 AVG 值的反引号,但其他所有内容都在您的问题上反引号。 @ektasingh
  • @ektasingh 我编辑了我的答案。看看有没有帮助。
  • 实际上它没有找到评分字段它给我的评分字段错误
  • 因为您的表中不存在ratings_average 字段。您必须使用rating 运行所有查询。这个AVG(rating) as ratings_average在返回查询结果时只会把字段名当作rating_average
【解决方案2】:

这个问题是一个 SQL 问题,它与作用域有关。我不太熟悉 laravel 的 API,但生成的 SQL(基于您的有用错误消息)是:

select `products`.*, AVG(rating) as ratings_average 
    from `products` left join `reviews` on `products`.`id` = `reviews`.`productID`
    where (`categoryID` in (9, 11, 31) and `productSellingPrice` between 50 and 5000
        and `ratings_average` between 1 and 2) group by `products`.`id`)

问题是ratings_average 计算列属于GROUP BY 范围。引用该列的唯一方法是在 HAVING 语句中。您的 SQL 语句如下所示:

select `products`.*, AVG(`ratings`.`rating`) as ratings_average
    where `products`.`id` = `reviews`.`productId`
    group by `products`.`id`
    having (`categoryID` in (9, 11, 31) and `productSellingPrice` between 50 and 5000
        and `ratings_average` between 1 and 2)

从技术上讲,上述having语句中的前两个子句可以在您的WHERE子句中,但ratings_average命名列只能在HAVING子句中引用。 WHERE 和 HAVING 都会限制您的结果,但 HAVING 会在分组发生后进行评估。

【讨论】:

  • 如果我使用有然后我在 grammer.php 中发现 strtolower() 期望参数 1 是字符串,给定对象的错误,我该如何解决我的问题
  • 我无法回答 API 特定的问题,只能回答需要生成的 SQL。正如我在回答中提到的,我并不精通 laravel 的 API。
猜你喜欢
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
  • 2022-01-07
相关资源
最近更新 更多