0

我的数据库架构如下

表 - X 有以下 3 列 docid(文档 ID)、术语(文档中的术语)、计数(特定 docid 的术语出现的术语数)

docid
terms
count

如何编写查询以查找术语列中同时包含“hello”和“hi”两个词的文档?

4

3 回答 3

1

试试这样...

Select DocId,Count(term) from Table Name
where Term='hello' or Term='hi'
Group by DocId 
Having Count(Distinct term)=2;
于 2013-05-16T09:44:05.370 回答
1
Select DocId
FROM TableName
where Term IN ('hello','hi')
Group by DocId 
Having Count(*)=2;

如果DISTINCT关键字在HAVING子句Term中不是唯一的,则更可取DocID

Select DocId
FROM TableName
where Term IN ('hello','hi')
Group by DocId 
Having Count(DISTINCT Term)=2;
于 2013-05-16T09:46:58.997 回答
1

尝试这个:

SELECT docid, COUNT(term)
FROM tablex
WHERE term IN('hello', 'hi')
GROUP BY docid
HAVING COUNT(DISTINCT term) = 2;

看看它的实际效果:

于 2013-05-16T09:47:42.613 回答