如何编写此查询以根据另一列的特定值显示一列的结果?

时间:2013-05-16 09:40:45

标签: sql

我的数据库架构如下

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

docid
terms
count

如何编写查询以查找在术语栏中包含“hello”和“hi”字样的文档?

3 个答案:

答案 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;

看到它的实际效果: