Simon
Simon

Reputation: 23151

mysql max function usage

the table videos has the folowing feels id,average,name how can i write the query, to select the name of video, which have the max average!!! i can do that vith two queries, by selecting the max(avege) from the table, and then find out the name, where ihe average equal to max!!! but i want to do that in one query!!! help me please!!!

Upvotes: 1

Views: 397

Answers (3)

Mike Sherov
Mike Sherov

Reputation: 13427

You don't need a group by for this, you just want to select the highest average!

SELECT * FROM videos ORDER BY average DESC LIMIT 1;

Upvotes: 2

Phil Ross
Phil Ross

Reputation: 26100

You can use an ORDER BY with a LIMIT:

SELECT id, average, name FROM videos ORDER BY average DESC LIMIT 1

ORDER BY average DESC orders the rows in order of descending average (i.e. the first row will have an average equal to MAX(average)). LIMIT 1 causes only the first row to be returned.

Upvotes: 0

jackbot
jackbot

Reputation: 3021

SELECT id,name,MAX(average) FROM videos;

All fields you choose to SELECT will be returned. Getting more data back is just a case of SELECTing more fields.

Upvotes: 0

Related Questions