假设我得到这张表:
MyTable
+----+-------+
| ID | count |
+----+-------+
| a | 2 |
| b | 6 |
| c | 4 |
| d | 6 |
| e | 2 |
+----+-------+
现在我想要回来:
Result
+----+-------+
| ID | count |
+----+-------+
| b | 6 |
| d | 6 |
+----+-------+
我想要具有最多计数值的ID。因此,如果有多个最大值,我想要它们全部。另外,我不知道 是否会有多个值,如果有,那么它将会是多少。
答案 0 :(得分:3)
您可以在子查询中获得最大的价值。例如,
SELECT *
FROM MyTable
WHERE count =
(
SELECT MAX(count)
FROM MyTable
)
答案 1 :(得分:1)
SELECT
Id, count
FROM MyTable
WHERE count = (SELECT MAX(count) FROM MyTable)
答案 2 :(得分:0)
select * from MyTable where count in (select max(count) from MyTable)
答案 3 :(得分:0)
select * from mytable
where
count= (select max(count) from mytable )
答案 4 :(得分:0)
试试这个
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
答案 5 :(得分:0)
您可以使用join而不是使用子查询
SELECT *,t.max_count
FROM Table1 t1
JOIN (SELECT MAX(`count`) max_count FROM Table1 ) t
HAVING t1.`count`=t.max_count
答案 6 :(得分:0)
试试这个:
select id, max(count) as max_count
from table1 where count=(select max(count) from table1)
group by id