繁体   English   中英

MySQL SELECT同一列中的多个/所有最大值

[英]MySQL SELECT multiple/all max values in the same column

假设我得到此表:

MyTable
+----+-------+
| ID | count |
+----+-------+
| a  | 2     |
| b  | 6     |
| c  | 4     |
| d  | 6     |
| e  | 2     |
+----+-------+

现在我要回来:

Result
+----+-------+
| ID | count |
+----+-------+
| b  | 6     |
| d  | 6     |
+----+-------+

我想要具有最多计数值的ID。 因此,如果有多个最大值,则全部都需要。 此外,我不知道是否会有多个值,如果有,有多少就可以。

您可以在子查询中获得最大的价值。 例如,

SELECT  *
FROM    MyTable
WHERE   count = 
        (
            SELECT  MAX(count) 
            FROM    MyTable
        )
SELECT
  Id, count
FROM MyTable
WHERE count = (SELECT MAX(count) FROM MyTable)
select * from MyTable where count in (select max(count) from MyTable)
select * from mytable 
where 
  count= (select max(count) from mytable )

尝试这个

select ID, count from table where count in (
    select distinct count from table order by Value desc limit 1
) order by count desc

您只请求了第一行,但是通过此查询,如果您想获得前三名,则可以轻松地将其更改为limit 3

您可以使用连接而不是使用子查询

SELECT *,t.max_count 
FROM Table1 t1
JOIN (SELECT MAX(`count`) max_count FROM Table1 ) t
HAVING t1.`count`=t.max_count

小提琴演示

尝试这个:

select id, max(count) as max_count 
  from table1 where count=(select max(count) from table1)
group by id

http://sqlfiddle.com/#!2/abb0b/9

暂无
暂无

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

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