I'm studying the tables that are provided by the Data Explorer for Stack Overflow. In the table Post
there is a column called AcceptedAnswerID
, but there is not an AcceptedAnswer
table. So where is the answer stored when a user replies to a question?
1 Answer
Since the Posts
table contains entries for both questions and answers (distinguished by their Posts.PostTypeId
), to get the accepted answer for the current question you'll have to perform a JOIN
on Posts.AcceptedAnswerId
and Posts.Id
, setting Posts.PostTypeId = 2
to get posts of type Answer
.
Below is the query to retrieve the answer count and the id of the accepted answer in raw data form, for the post with the given id:
SELECT Posts.AnswerCount, Posts.AcceptedAnswerId
FROM Posts
WHERE
Posts.Id = 19628880;
You can find the whole query, forkable and modifyable, on Data Explorer here.