Why is this query (potentially) only returning posts that are questions?

John Leehey

I'm trying to write a simple query in Data Explorer to find out the number of answers a user has for a certain tag. Here's what I have so far (also here):

SELECT Count(*)
FROM Users u
   JOIN Posts p ON p.owneruserid = u.id
   JOIN PostTags pt ON pt.PostId = p.Id
   JOIN Tags t  ON t.Id = pt.TagId
WHERE u.Id = ##UserId##
     AND t.TagName IN 
     (
       ##Tag1:string##
     )

The problem is that this is only returning posts that are questions. I've tried modifying the WHERE clause to:

WHERE u.ID = ##UserId## AND p.postTypeId = 2 ...

To only return posts that are answers, but it returns a count of 0, which is leading me to believe that my JOINs are only resulting in posts that are questions. Can anyone see why this is happening, and how to modify this query to show answers as well?

FYI the ultimate goal is to try to see how many more answers are needed for a tag bronze/silver/gold badge.

Krzysztof

It seems that tags are not added to answers, so you should check tags on question:

SELECT Count(*)
FROM Users u
   JOIN Posts p ON p.owneruserid = u.id
   JOIN Posts q ON q.Id = p.ParentId
   JOIN PostTags pt ON pt.PostId = q.Id
   JOIN Tags t  ON t.Id = pt.TagId
WHERE u.Id = ##UserId## AND t.TagName IN (##Tag1:string##)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Query only returning 7 of 9 expected values

분류에서Dev

Linq query returning duplicates

분류에서Dev

Query returning false

분류에서Dev

Query_posts wrap every 6 posts together

분류에서Dev

Why is this not returning an @@ERROR?

분류에서Dev

wordpress query_posts 대안

분류에서Dev

Delete postmeta and posts with one SQL query

분류에서Dev

Query WordPress Posts and put Titles in Array

분류에서Dev

WP query to get posts from category ( not child of it )

분류에서Dev

double function only returning -0

분류에서Dev

Returning a PHP response in a AJAX query

분류에서Dev

MongoDb query not returning all the fields

분류에서Dev

MongoDb query not returning all the fields

분류에서Dev

WMIC Query returning unexpected answer?

분류에서Dev

Why do some 'potentially dangerous Request.Path' HttpExceptions ignore httpErrors settings?

분류에서Dev

Why is diff returning an incorrect value?

분류에서Dev

Why this script is returning ' 'null' is not an object'?

분류에서Dev

Why is Scrapy returning duplicate results?

분류에서Dev

Query posts from two post formats within a category

분류에서Dev

How to display Posts in wordpress page using mysql query?

분류에서Dev

returning only the most recent record in dataset

분류에서Dev

scraper only returning results for first 2 inputs

분류에서Dev

regex scan only returning first value

분류에서Dev

Using QualifierFilter leads to only returning matched columns

분류에서Dev

allDocs query returning design docs in PouchDB

분류에서Dev

MySQL MATCH AGAINST query not returning certain requests

분류에서Dev

Returning null values with multiple table in a query

분류에서Dev

Linq to sql query not returning correct items

분류에서Dev

LINQ to XML query Where() function not returning anything

Related 관련 기사

뜨겁다태그

보관