我需要找到最大计数的行。只有一张桌子,所以应该很容易,但它不是。 以下是表格的内容:
+------+------+------+
| row1 | row2 | row3 |
+------+------+------+
| 3 | 2 | 1 |
| 6 | 4 | 5 |
| 6 | 2 | 1 |
+------+------+------+
我需要找到row1的最大计数项: 使用: SELECT COUNT(*)AS c,row1 AS名称 从绘制 GROUP BY名称; 给我结果:
+------+------+
| c | name |
+------+------+
| 1 | 3 |
| 2 | 6 |
+------+------+
但我想只显示一行最多" c":
+------+------+
| c | name |
+------+------+
| 2 | 6 |
+------+------+
使用: SELECT MAX(c),name( SELECT COUNT(*)AS c,row1 AS名称 FROM绘制GROUP BY名称 )AS计数;
给我结果:
+------+------+
| c | name |
+------+------+
| 2 | 3 |
+------+------+
这意味着它给出了最大计数(c),但在名称行中给出了第一个数字。
有办法解决吗?
答案 0 :(得分:1)
我认为这就是你想要的,但你的要求有点不清楚。如果这是任何其他平台,你可以使用窗口函数会更好。
SELECT *
FROM (
SELECT COUNT(*) AS c, row1
FROM draw
GROUP BY row1
) as Sub1
WHERE Sub.c = (
SELECT Max(c)
FROM (
SELECT COUNT(*) AS c
FROM draw
GROUP BY row1
) as Sub2
) as Sub3
答案 1 :(得分:-1)
select top 1 row1, count(*)
from draw
group by row1
order by count(*) desc
按计数递减排序(即最高计数优先)并仅取第一个。
答案 2 :(得分:-1)
你可以试试这个
SELECT COUNT( `row1` ) AS c, `row1`
FROM `draw`
GROUP BY `row1`
ORDER BY `row1` DESC
LIMIT 1