Добрый день уважаемые.
Всю голову сломал, не могу составить нижеуказанный запрос на Laravel Query Builder.
Помогите пожалуйста.
SELECT products.title, products.price, x.sales
FROM products
INNER JOIN
(
SELECT product_id,
MAX(sales) AS sales
FROM offers
GROUP BY product_id
) x
ON x.product_id = products.id
ORDER BY x.sales DESC LIMIT 20
Пробовал так :
$populars = DB::table('products')
/* ->select(DB::raw(' JOIN (SELECT product_id, MAX(sales) AS sales FROM offers GROUP BY product_id) ON offers.product_id = products.id ')) */
->select( DB::table('offers')
->join('offers.product_id', '=', 'products.id')
->select('product_id')
->max('sales')
->groupBy('offers.product_id')
->get()
)
->orderBy('offers.sales', 'desc')
->take(20)
->get();
но выдаёт ошибку :
SQLSTATE[42000]: Syntax error or access violation: 1049 Unknown database 'offers' (SQL: select max(`sales`) as aggregate from `offers` inner join `offers`.`product_id` on `=` = `products`.`id`)