我的数据库架构如下
表 - X有以下3列docid(文档ID),术语(文档中的术语),count(特定docid术语出现的术语数)
docid
terms
count
如何编写查询以查找在术语栏中包含“hello”和“hi”字样的文档?
答案 0 :(得分:1)
试试这个......
Select DocId,Count(term) from Table Name
where Term='hello' or Term='hi'
Group by DocId
Having Count(Distinct term)=2;
答案 1 :(得分: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;
答案 2 :(得分:1)
试试这个:
SELECT docid, COUNT(term)
FROM tablex
WHERE term IN('hello', 'hi')
GROUP BY docid
HAVING COUNT(DISTINCT term) = 2;
看到它的实际效果: