我正在尝试搜索具有给定标签的问题。
如何解决以下问题?
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;
我跑并得到
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
我显然应该使用 JOIN。
但是,我不想将标签作为最终结果的输出。
try:
SELECT q.question_id, q.title
FROM questions q
INNER JOIN tags t ON q.question_id=t.question_id
WHERE tag = $1
ORDER BY q.was_sent_at_time
DESC LIMIT 50
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)