php - 当我加入另一个表时发现查询错误

标签 php mysql sql laravel model-view-controller

当我将一张表连接到另一张表时出现此错误。我输入 as 关键字但它不起作用它给我错误,如未找到字段我该如何解决此错误

代码:

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`)

最佳答案

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

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

关于php - 当我加入另一个表时发现查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35035412/

相关文章:

mysql - MySQL 中的 BLOB 到长文本转换

sql - 在包中找不到 SSIS 连接

sql - 将数据从 Excel 用户窗体复制到 Access 表时出现语法错误

sql - 从模式生成 Rails 迁移

php - 格式化金额为数据库格式

php - MySQL,对 TableView 的列求和

php - 开源项目 PHP 语法

php - PHP 中的 eBay API 调用返回 'The item specific Brand is missing' 错误

python - blender Python MySQL

mysql - 当用户 id 没有出现在另一个表中时的 sql 条件