当我将一张表连接到另一张表时出现此错误。我输入 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`)
最佳答案
您可以找到 $minRating
和 maxRating
而不是评级数组。通过这两个值,您可以像这样运行查询:
$query->whereBetween('rating',[$minRating,$maxRtaing]);
关于php - 当我加入另一个表时发现查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35035412/