MySQL在同一列中选择多个/所有最大值
问题描述:
假设我得到这张表:
MyTable
+----+-------+
| ID | count |
+----+-------+
| a | 2 |
| b | 6 |
| c | 4 |
| d | 6 |
| e | 2 |
+----+-------+
现在我想要这个:
Result
+----+-------+
| ID | count |
+----+-------+
| b | 6 |
| d | 6 |
+----+-------+
我想要具有最多计数值的 ID.所以如果有多个最大值,我想要它们.另外我不知道是否会有多个值,如果有,会有多少个.
I want the IDs that have the most counted values. So if there are multiple maximum values, I want them all. Also I don't know if there will be multiple values and if there are, how many it will be.
答
可以在子查询中获取最大的值.例如,
You can get the greatest value inside the subquery. Eg,
SELECT *
FROM MyTable
WHERE count =
(
SELECT MAX(count)
FROM MyTable
)