I am trying to search questions which have a given tag.
How can you fix the following problem?
Tables
questions | tags
-------------------|-----------------
question_id | tag
title | question_id
was_sent_at_time |
My code
SELECT question_id, title
FROM questions
WHERE question_id IN
(
SELECT question_id
FROM questions
ORDER BY was_sent_at_time // problem here
DESC LIMIT 50
)
AND tag IN // problem here
(
SELECT tag FROM tags
WHERE tag = $1
AND WHERE question_id IN (
SELECT question_id
FROM questions
ORDER BY was_sent_at_time
DESC LIMIT 50
)
)
ORDER BY was_sent_at_time
DESC LIMIT 50;
I run and get
Warning: pg_prepare() [function.pg-prepare]: Query failed: ERROR: syntax error at or near "WHERE" LINE 14: AND WHERE question_id IN ( ^ in /var/www/codes/handlers/searches/handle_questions_by_tag.php on line 30
I apparently should be using JOINs. However, I do not want to get tags as an output to my final result.