我正在匹配文本与关键字。我需要返回包含两个关键字(4)和(7)的所有文本:
TextID KeywordID
2 4
2 7
3 4
4 4
5 4
5 7
6 4
6 7
7 4
7 7
8 4
9 4
10 4
10 7
11 4
12 4问题是如何排除不同时包含文本in 3、4、8、9、11的文本(它们不应该出现在结果中)?
任何帮助都将不胜感激!
发布于 2017-03-21 22:15:34
假设您没有重复的textId-KeywordId对,下面应该可以工作:
SELECT textid
FROM table
WHERE keywordId in (4,7)
GROUP BY textid
HAVING COUNT(*) >= 2如果您有dups,您可以使用count(distinct keywordId)作为@Gordon的答案。
更新这里的MS Access查询:
SELECT tblPerformanceKeyword.TextID
FROM tblPerformanceKeyword
WHERE tblPerformanceKeyword.KeywordID = 4 Or tblPerformanceKeyword.KeywordID = 7
GROUP BY tblPerformanceKeyword.TextID
HAVING COUNT(tblPerformanceKeyword.KeywordID) >= 2;发布于 2017-03-21 22:13:15
一种方法是使用group by和having。
select textid
from t
where keywordid in (4, 7)
group by textid
having count(*) = 2;如果表可以有重复项,则使用count(distinct keywordid)。
https://stackoverflow.com/questions/42939109
复制相似问题