MYSQL: how to select the largest value from a column?

I have a mysql table with a list of movies. I would like to show the movie with the most votes or occurrences on a web page using php. How can i do this?

title
Toy Story 3
Toy Story 3
Toy Story 3
Inception
Inception 

For example: the value to be displayed will be Toy Story 3

+3
source share
4 answers
select title, count(*) as cnt from films group by title order by cnt desc limit 1
+8
source
SELECT title, count(*) as count
FROM films
GROUP BY title
ORDER BY count DESC
LIMIT 1

This will count the number of rows for each heading and return only the highest heading. Remove the restriction if you want all the titles to be sorted by the maximum number of titles.

This query will return something like:

+-------------+-------+
| title       | count |
+-------------+-------+
| Toy Story 3 |     3 |
+-------------+-------+
+3
source
SELECT title,COUNT(title) AS cnt
 FROM Film
 GROUP BY title
 HAVING cnt=(SELECT COUNT(title) as count
              FROM Film
             GROUP BY title
             ORDER BY count desc limit 1);

, .

+2
select f.title, f.cnt as count 
from
( select title, count(*) as cnt
from films f
group by title ) f
, ( select max(count(*)) mcnt
FROM films 
GROUP BY title ) f2
where f.cnt = f2.mcnt

,

+1

Source: https://habr.com/ru/post/1785044/


All Articles