如何获取MySQL中包含最大值的行数据?

How to get the row data that include max value in MySQL?

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

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

请帮帮我!

您没有提及您使用的 MySQL 版本,所以我假设它是现代版本 (8.x)。您可以使用 ROW_NUMBER() window 函数来确定您需要的行。

例如:

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)
)