我有一个名为movies
的表和一个名为movie_id
的列。我想在表格中找到最常见的movie_id
。
列中的示例数据:
movie_id
234
343
2928
956
73
234
234
所以结果应该是234,因为它的行数最多。我从来没有尝试过,所以我真的不知道如何实现这一目标。
答案 0 :(得分:4)
取决于sql可能类似
SELECT * FROM movies
WHERE
movie_id=(SELECT movie_id FROM movies GROUP BY movie_id ORDER BY count(*) DESC LIMIT 1)
也就是说,如果我理解你正在寻找什么。
答案 1 :(得分:3)
对于MySQL:
SELECT movie_id
FROM movies
GROUP BY movie_id
ORDER BY COUNT(*) DESC
LIMIT 1
对于SQL-Server:
SELECT TOP 1
movie_id
FROM movies
GROUP BY movie_id
ORDER BY COUNT(*) DESC
如果您想要返回所有并列(顶部)电影:
SELECT movie_id
FROM movies
GROUP BY movie_id
HAVING COUNT(*) =
( SELECT TOP 1 COUNT(*)
FROM movies
GROUP BY movie_id
ORDER BY COUNT(*) DESC
)
答案 2 :(得分:0)
SELECT movie_id FROM (
SELECT
movie_id,
count(*) AS num
FROM <tablename>
GROUP BY movie_id
) AS baseview
ORDER BY num DESC
LIMIT 1
答案 3 :(得分:0)
对于 SQL-Server ,请使用:
SELECT TOP 1 COUNT(*) as cnt
FROM movies
GROUP BY(movie_id)
ORDER BY cnt DESC