【发布时间】:2021-03-07 10:51:05
【问题描述】:
我有一个包含一些名称和一些数字的数据库表,如下所示:
first last number
max muster 1
max juster 2
max huster 3
jen muster 4
jen jenker 5
ian hoster 6
...
我通过以下方式查询最常见的名字:
SELECT first, COUNT(*) AS value FROM table
GROUP BY first
ORDER BY COUNT(*) DESC
LIMIT 3
我想知道这个数据库表的名字“ian”的排名。在这种情况下,它是第三个常见的名字,上面的查询给了我以下信息:
first value
1 max 3
2 jen 2
3 ian 1
我想要的是以下代码:
first value
3 ian 1
或类似的东西,这样我就可以通过给出first='ian' 来达到数字“3”,因为它是我表中的第三个通用名称。我该如何查询呢?
示例:
SELECT first, COUNT(*) AS value FROM table
GROUP BY first
ORDER BY COUNT(*) DESC
# So far we ordered the list from most common to least common
FILTER WHERE first='ian'
# We filtered the other names so that only first='ian' stays in the query,
# and we did not lose the index value (in this case 3) of the 'ian'
这行不通,但我想你明白我在寻找什么。
【问题讨论】:
-
我不会像stackoverflow.com/questions/16568/…的问题那样选择/搜索行我搜索一个名字并想学习名字的行
标签: sql count sql-order-by where-clause rank