-1

如何获取具有 max(reg_count) 的总行数据。当我尝试这个时,它只给了我最大的 reg_count 值。我想获取每个专辑 ID 中具有最大 reg_count 的整行数据。(所以像这样:4 Bemisal ha 1 1 8) - 总共 4 行

SELECT albumID, max(reg_count) as max_count
FROM contentnew 
GROUP BY albumID

在此处输入图像描述

请帮我!

4

1 回答 1

2

你没有提到你正在使用的 MySQL 版本,所以我假设它是一个现代版本(8.x)。您可以使用ROW_NUMBER()窗口函数来识别您需要的行。

例如:

select *
from (
  select *,
    row_number() over(partition by albumID order by reg_count desc) as rn
  from contentnew
) x
where rn = 1

在 MySQL 5.x 中,您可以使用相关子查询:

select *
from contentnew a
where a.reg_count = (
  select max(reg_count) 
  from contentnew b 
  where b.albumID = a.albumID)
)
于 2020-04-28T13:01:25.293 回答