0

我有一个数据库,其中一张表有 3 列。这是 .schema 给出的: .schema

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

我正在尝试一个连接查询来获取两个特定的 docid 并按术语匹配它们,(对不起,如果它没有意义,我是 sql 新手)。我期待一个格式为:

docid1 |term |count
docid2 |term |count

但我得到的结果格式docid1 |term |count|docid2 |term |count 是我做错了什么?

还尝试选择 docid 或任何其他列而不是执行 select* 会给我一个模棱两可的键错误。这是我正在使用的查询:

 select* from (select docid, term, count from frequency where docid = '10080_txt_crude') as c join (select docid , term, count from frequency where docid = '17035_txt_earn') as d on c.term = d.term;

最后在最后添加 group by docid 只会给出一个答案。

4

2 回答 2

1
SELECT docid,term,count
FROM Frequency
WHERE docid=docid1 or docid=docid2
GROUP BY docid,term,count
HAVING COUNT(*) >1 
于 2013-05-16T12:02:46.513 回答
0

尝试从一个表select c.* from ...中选择所有列,而不是从所有连接表中选择所有列。

select c.* from 
    (select docid, term, count from frequency where docid = '10080_txt_crude') as c 
join 
    (select docid , term, count from frequency where docid = '17035_txt_earn') as d 
on c.term = d.term;

或者,也许您可​​以进一步简化语法?

select c.docid,c.term,c.ount 
from frequency c
join frequency d on c.term = d.term
where c.docid = '10080_txt_crude' and
    d.docid = '17035_txt_earn';

随之而来的问题是,您实际上在哪里使用 D。

你想做select * from frequency where docid='10080_txt_crude' or docid='17035_txt_earn'什么?


连接两个 DocID 之间的匹配项:

select c.docid as c_docid, d.docid as d_docid, c.term, c.count, d.count
from frequency c
join frequency d on c.term = d.term
where c.docid = '10080_txt_crude' and
    d.docid = '17035_txt_earn';

这将产生 (C DocID, D DocID, Term (equal in C and D) , C Count, D Count)。

于 2013-05-16T12:02:05.047 回答