[英]How to get the MAX value in MySQL?
你可以試試
select max(reg_count) from contentnew group by albumID
分組后,您可以在每個組上應用聚合函數,例如 max。 在您的示例中嘗試:
SELECT albumID, max(reg_count) as max_count
FROM contentnew
GROUP BY albumID
這將使用組中的 max_count 投影每個專輯 ID。 在 select 語句中,您只能使用聚合函數。 我們能夠投影(或打印)albumID 的原因是因為這是我們分組的列。
以下評論:
SELECT *
FROM contentnew as c1
WHERE c1.reg_count < (
SELECT max(c2.reg_count)
FROM contentnew as c2
WHERE c1.albumID = c2.albumID
GROUP BY c2.albumID)
你快到了,如果你想要表格中的每一列,可能有用的一件事是使用 row_number() function。
with contentnew_test
as
(
select row_number() over (partition by albumId order by reg_count desc) row
,* from
contentnew
)
select * from contentnew_test where row = 1 order by reg_count desc;
我將此用作參考,因為不確定語法https://www.mysqltutorial.org/mysql-window-functions/mysql-row_number-function/
子查詢會給你一個類似這樣的結果集:
row albumId reg_count ...
1 1 8 ...
2 1 7 ...
3 1 3 ...
4 1 1 ...
1 2 22 ...
2 2 9 ...
3 2 6 ...
4 2 1...and so on.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.