MySQL获得的不同字段的数量经常发生在“x”之外

时间:2017-11-10 16:34:29

标签: mysql

我有两个表,一个word表和一个document表。下面的查询用于获取单词列表以及每个单词在特定语言的所有文档中出现的次数。

select body, count(*) as count from word
join document on word.document_id = document.id
where document.language = 'english'
group by body
order by count(*) desc

它会产生如下所需的结果:

body    count
-------------
and     44
the     42
a       28
in      24

如何获得比任何给定单词更常见的单词数?根据单词in,如何查询获取3,因为在我的文档中,三个单词比in更常见?

2 个答案:

答案 0 :(得分:3)

试试这个:

SELECT @rank:=@rank, body, counter
FROM
(
SELECT body, COUNT(*) AS counter
FROM word
JOIN document ON word.document_id = document.id
WHERE document.language = 'english'
GROUP BY body
ORDER BY counter desc
) t, (SELECT @rank := 0) r;

编辑:如果您想获得数量比ex.'in'更高的单词数量

SELECT @rank:=@rank AS rank
FROM
(
SELECT body, COUNT(*) AS counter
FROM word
JOIN document ON word.document_id = document.id
WHERE document.language = 'english' and body = 'in'
GROUP BY body
ORDER BY counter desc
) t, (SELECT @rank := 0) r;

答案 1 :(得分:0)

我得到了它,但我想知道是否有更好的方法。

select count(*) + 1 as rank
from (
    select body, count(*) as count
    from word
    join document on word.document_id = document.id
    where document.language = 'english'
    group by body
) t
where count > (select count
from (
    select body, count(*) as count
    from word
    join document on word.document_id = document.id
    where document.language = 'english'
    group by body
) t
where body = 'a')

一个问题是,如果您查询不存在的单词,则会显示rank = 1。