V
V
Vladislav Nagorny2017-02-01 20:00:18
MySQL
Vladislav Nagorny, 2017-02-01 20:00:18

How to solve the problem of order by and group by?

Good afternoon
There is a base with a product and a user.
Need a list to display the most expensive products of users

Product::orderBy('price','DESC')
            ->groupBy('user_id')->get();

As a result, I get incomplete information where the most expensive ones are sold. How can I modify sql to make group by work correctly?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2017-02-01
@esvils

Since price is not included in the aggregate function, the first available value is taken when grouping. Use MAX(price).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question