0

I have this MySQL query that can get the maximum value from all rows in my table, it's working though but the problem is how can I get the other columns from the row that has the maximum value? The record_id that shows in the result is from the first row not from the row that has the maximum value.

Query:

SELECT `record_id`, MAX(download_count) as `max_dl` FROM `downloads`

Result:

+-----------+--------+
| record_id | max_dl |
+-----------+--------+
|        13 |      5 |
+-----------+--------+
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • 1
    `SELECT record_id, download_count as max_dl ORDER BY download_count DESC LIMIT 1` to get `record_id` of row where `download_count` is max. – Umair Khan Oct 14 '20 at 06:07
  • `SELECT MAX(record_id) as record_id, MAX(download_count) as max_dl` highest `record_id` and highest `download_count`. – Umair Khan Oct 14 '20 at 06:08

1 Answers1

0

You can use a subquery to list all the records with the maximum value:

 SELECT 'record_id', download_count as 'max_dl' FROM 'downloads' WHERE download_count = (SELECT MAX(download_count) FROM 'downloads')
salyangoz
  • 101
  • 6