我有一个数据库,其中有一个表有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;
最后,最后通过docid添加group只能给出一个答案。
答案 0 :(得分:1)
SELECT docid,term,count
FROM Frequency
WHERE docid=docid1 or docid=docid2
GROUP BY docid,term,count
HAVING COUNT(*) >1
答案 1 :(得分: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 (在C和D中相等),C Count,D Count)。