我需要一个查询来从数据库中获取前 3 个计数,但使用 max 只会从中获取前 3 个计数。如何获得前 3 名的计数?
count
30
29
29
28
28
28
27
26
25
SELECT count FROM tableName WHERE count = max($count) LIMIT 30 //doesn't work because max only get the top first count.
预期结果
30
29
29
28
28
28
最佳答案
子查询选择最高的 3 个不同值。在本例中,它们是 30、29 和 28,外部查询选择具有这些计数之一的所有行。
select `count` from tablename
where `count` in (
select distinct `count`
from tablename
order by `count` desc
limit 3
)
顺便说一下,count
是一个保留字,您应该尽量避免将其用作列名。
关于php - 获取数据库的前 3 个计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811364/