我有以下sql查询,以获取未被注意的问题,其中不包含除我最喜欢的标签之外的其他标签,此外它还包含更多过滤器。
此查询存在两个主要问题(可能需要更多改进)
distinct
,因为即使我没有使用任何左连接,它也会给我重复结果。如何在不使用不同的关键字Select distinct top 100
'http://stackoverflow.com/questions/'+Cast(p.Id as varchar(20)) as ids
from Posts p
Join posttags pt on p.Id=pt.PostId
where AcceptedAnswerId is null
and AnswerCount = 0
and len(body) <2000
and viewCount<30
and DateDiff(hour, p.creationDate, GETDATE())<200
and ClosedDate is null
and p.id not in
(
select p.id as id from posts p join posttags pt
on p.Id=pt.PostId
where pt.tagId != 21 and pt.tagId != 3
and pt.tagId != 9 and pt.tagId != 5
and pt.tagId != 820 and pt.tagId != 2
and pt.tagId != 22 and pt.tagId != 1508
and pt.tagId != 46426 and pt.tagId != 96
and pt.tagId != 363
and AcceptedAnswerId is null
and AnswerCount = 0
and len(body) <3000
and viewCount<30
and DateDiff(hour, p.creationDate, GETDATE())<200
and ClosedDate is null
)
order by ids
--21 mysql --3 javascript --9 c# --5 php --820 jquery
--2 html --22 sql --1508 json --46426 nodejs--96 asp.net
--363 ajax
答案 0 :(得分:2)
如果HAVING
是您需要的唯一列,并且ID
可以避免同一列上的多个条件,则可以使用IN()
子句:
Select distinct top 100
'http://stackoverflow.com/questions/'+Cast(p.Id as varchar(20)) as ids
from Posts p
Join posttags pt
on p.Id=pt.PostId
where AcceptedAnswerId is null
and AnswerCount <3
and len(body) <2000
and viewCount<30
and DateDiff(hour, p.creationDate, GETDATE())<200
and ClosedDate is null
GROUP BY 'http://stackoverflow.com/questions/'+Cast(p.Id as varchar(20))
HAVING COUNT(*) = SUM(CASE WHEN tagID IN(21,3,9,820,2,22,1508,46426,96,363) THEN 1 ELSE 0 END)
COUNT(*)
将返回此ID
的记录总数,SUM(CASE..)
将返回没有不需要的标记的记录数。如果它们相等,则意味着只存在所需的标签。