mysql - How do I use MAX() to return the row that has the max value? -
i have table orders
fields id
, customer_id
, amt
:
and want customer_id
largest amt , value of amt
.
i made query:
select customer_id, max(amt) orders;
but result of query contained incorrect value of customer_id
.
then built such query:
select customer_id, max(amt) maximum orders group customer_id order maximum desc limit 1;
and got correct result.
but not understand why first query not worked properly. what doing wrong?
and possible change second query obtain necessary information me in simpler , competent way?
mysql
allow leave group by
off of query, returning max(amt)
in entire table arbitrary customer_id
. other rdbms require group by
clause when using aggregate.
i don't see wrong 2nd query -- there other ways it, yours work fine.
Comments
Post a Comment