How can you select question_id and tags for a question when one of the tags is known?
I am building a tag-search which searches questions by one tag such that the result shows the question and its tags including the one which was used in the search.
I use the same tables as in this question.
They are
Tables
questions | tags
-------------------|-----------------
question_id | tag
title | question_id
was_sent_at_time |
The query
SELECT question_id, tag
FROM tags
WHERE question_id IN
(
SELECT question_id
FROM questions
ORDER BY was_sent_at_time
DESC LIMIT 50
)
AND tag = $1; // Problem here
The problem with this query is that it does not show other tags assigned to the question.
It may be possible to get the question_id and tags if there exists a given tag.
That answers your question (if I understood it correctly), but I think you will have too much duplicate data – you don’t really want the question title repeated for each tag. So you should break it out into 2 result sets:
and:
Then when you build your page or whatever, you would bring them together.