简体   繁体   English

如何获取表MySql中每个项目的最大数

[英]How to get the largest number for each item in the table MySql

How to get the largest number for each item in the table MySql如何获取表MySql中每个项目的最大数

在此处输入图像描述

I want to have the most chosen name "name_p" everywhere "num_place"我想在“num_place”处都有选择最多的名字“name_p”

SELECT name_p,num_place,COUNT(`id`) as couny_pla FROM selec_user GROUP BY name_p,num_place ORDER BY selec_user.num_place ASC 

this result:-这个结果:-

在此处输入图像描述

But this is the result I needed但这是我需要的结果

在此处输入图像描述

This can produce your desired result but may arbitrarily either 'e' or 'h' for num_place 3, since they are tied:这可以产生您想要的结果,但对于 num_place 3 可能是任意的“e”或“h”,因为它们是绑定的:

select name_p, num_place, couny_pla
from (
    select name_p, num_place, row_number() over (partition by num_place order by couny_pla desc) n, count(*) couny_pla
    from selec_user
    group by name_p, num_place 
) ranked_groups
where n=1
order by num_place;

Replace row_number() with rank() if you want all tied values returned.如果要返回所有绑定值,请将row_number()替换为rank()

fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM