【发布时间】: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