Self Join在SQLite中返回不同的记录

时间:2013-05-14 03:45:22

标签: sql sqlite self-join

我试图使用以下SQL语句检索不同的记录:

SELECT count (*) FROM(
SELECT DISTINCT pv1.docid, pv2.docid 
FROM Frequency pv1
INNER JOIN Frequency pv2
ON pv1.docid = pv2.docid
AND pv1.term="transactions"
AND pv2.term="world")x;

架构是:

CREATE TABLE Frequency (
docid VARCHAR(255),
term VARCHAR(255),
count int,
PRIMARY KEY(docid, term);

我知道有187个文档包含term world,26个包含事务。

1 个答案:

答案 0 :(得分:1)

你只是想这样做吗?

SELECT 
    COUNT(DISTINCT pv1.[docid])
FROM
    Frequency AS pv1
JOIN
    Frequency AS pv2
        ON pv2.[docid] = pv1.[docid]
WHERE
    pv1.[term] = "transactions"
    AND pv2.[term] = "world"

这将返回具有您规定条件的唯一docid的计数。