Answer the question
In order to leave comments, you need to log in
Compose a query on Laravel Query Builder?
Good afternoon dear ones.
I broke my whole head, I can’t compose the following query on Laravel Query Builder.
Help me please.
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`)
Answer the question
In order to leave comments, you need to log in
Thank you.
I wrote incorrectly about the work of collections in the model.
here is the solution:
$populars = DB::table('products')
->join('offers', 'products.id', '=', 'offers.product_id')
->orderBy('offers.sales', 'desc')
->get()
/* */
->unique('title')
->take(20);
return $populars;
$results = DB::select( DB::raw("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") );
you have an error in the join syntax, try this:
if anything, here is a link to the place in the dock that you need
https://laravel.com/docs/5.8/queries#joins
See what picture.
First, there is Progressive jpeg
.
In many cases, a png is much smaller than a jpeg (if there are large monochrome areas, for example).
Or, if the design allows, you can replace the image with a vector one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question