User abbood (myself) has eight posts (in total) that have the tag cocoapods
; six questions and two answers.
I thought that this query should simply get all eight:
SELECT Count(*)
FROM posts p
JOIN posttags pt
ON p.id = pt.postid
JOIN tags t
ON t.id = pt.tagid
WHERE owneruserid = 766570
AND t.tagname = 'cocoapods'
but instead it only got six.
It turns out that posts that are answers are not associated with tags. So for example this query
SELECT *
FROM posttags
WHERE postid IN (SELECT p.id AS 'post id'
FROM posts p
WHERE p.owneruserid = 766570
AND p.posttypeid = 1) // posttypeid = 1 is a question
returns many results, whereas this query
SELECT *
FROM posttags
WHERE postid IN (SELECT p.id AS 'post id'
FROM posts p
WHERE p.owneruserid = 766570
AND p.posttypeid = 2)
returns zero results.
How can I get the total number of posts (both questions and answers) for a specific user for a specific tag?