如果它具有特定标记,则仅获取记录

时间:2016-09-05 11:31:44

标签: sql sql-server

我有以下sql查询,以获取未被注意的问题,其中不包含除我最喜欢的标签之外的其他标签,此外它还包含更多过滤器。

Live Demo

此查询存在两个主要问题(可能需要更多改进)

  1. 我已经应用了一种虚假技术来实现 in <和strong> =&gt; (你可以看到查询。我必须先使用相同的查询两次才能得到我需要的东西,然后使用非过滤器过滤相同的结果,忽略所有其他标签而不是我最喜欢的标签的问题)。我找不到别的办法。
  2. 我已应用distinct,因为即使我没有使用任何左连接,它也会给我重复结果。如何在不使用不同的关键字
  3. 的情况下区分ID
    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
    

1 个答案:

答案 0 :(得分:2)

如果HAVING是您需要的唯一列,并且ID可以避免同一列上的多个条件,则可以使用IN()子句:

点击Response waiting questions

查看现场演示
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..)将返回没有不需要的标记的记录数。如果它们相等,则意味着只存在所需的标签。