我正在嘗試查詢每十年評分最高的電影(或者如果有 2 部評分最高(最大)的電影)。我快到了,我唯一的問題是,如果十年內有 2 部電影(評級相同(最高評級)),它不會查詢它。我嘗試了很多不同的東西,但似乎沒有任何效果。到目前為止我得到了
SELECT FLOOR(premiered / 10) * 10 AS Decades,
title,
rating
FROM titles
INNER JOIN
ratings ON titles.title_id = ratings.title_id
GROUP BY decades
回傳:
1920 The Kid 8.3
1930 City Lights 8.5
1940 It's a Wonderful Life 8.6
1950 12 Angry Men 9
1960 The Good, the Bad and the Ugly 8.8
1970 The Godfather 9.2
1980 Star Wars: Episode V - The Empire Strikes Back 8.7
1990 The Shawshank Redemption 9.3
2000 The Lord of the Rings: The Return of the King 9
2010 Inception 8.8
2020 Jai Bhim 8.9
我的架構看起來像:
標題
title_id
title
premiered -> this is the year of movie's release
收視率
title_id
rating
我不確定如何獲得每十年(sqlite)出現的所有最大(評級)。我想要的結果是得到這樣的東西
1920 The Kid 8.3
1920 Another_movie_with_matching_max(rating) 8.3
編輯:Jarlh 建議使用子查詢來獲得每十年的最高評分。我想到了
SELECT FLOOR(premiered / 10) * 10 AS Decades,
rating as rat
FROM ratings
JOIN
titles ON ratings.title_id = titles.title_id
GROUP BY decades
HAVING max(rating)
現在我只是不確定如何使用這個子查詢來獲取所有電影。我試過->
SELECT FLOOR(premiered / 10) * 10 AS Decades,
title,
rating
FROM titles
INNER JOIN
ratings ON titles.title_id = ratings.title_id
where decades and RATING = (
SELECT FLOOR(premiered / 10) * 10,
rating as rat
FROM ratings
JOIN
titles ON ratings.title_id = titles.title_id
GROUP BY FLOOR(premiered / 10) * 10
HAVING max(rating)
)
GROUP BY decades
哪個不按預期作業
uj5u.com熱心網友回復:
我想到了。必須使用 IN() 而不是將兩個表連接在一起。
SELECT FLOOR(premiered / 10)*10 AS str AS decades, rating, title
FROM ratings
JOIN titles ON titles.title_id = ratings.title_id
WHERE (decades, rating) IN
(SELECT FLOOR(premiered / 10)*10AS decades, MAX(rating)
FROM ratings
JOIN titles ON titles.title_id = ratings.title_id
GROUP BY decades) ORDER BY decades ASC;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511661.html
標籤:sqlsqlite
