我目前有以下MySQL查询,执行正常,没有任何错误:
SELECT topic_id,
topic_title,
topic_author,
topic_type
FROM forum_topics
WHERE ( ( ( forum_id = '2' )
OR ( forum_id != '4'
AND topic_type = 2 ) )
AND deleted = 0 )
ORDER BY topic_id DESC
然而它也没有按照我的意图行事,我希望它返回主题的所有结果,其中forum_id为2并且删除等于0以及返回topic_id不等于4且topic_type为的主题的结果2,删除等于0(如果存在)。
但是目前它正在做第一次只返回主题的结果,其中forum_id是2并且删除等于0而不是另一个(即使它们存在!:/)。
我相信我做错了什么......
非常感谢所有帮助。
答案 0 :(得分:1)
为了使自己更简单,首先放置查询的一致部分(deleted = 0):
SELECT
topic_id,
topic_title,
topic_author,
topic_type
FROM forum_topics
WHERE deleted = 0 AND
( ( forum_id = '2' )
OR ( forum_id != '4'
AND topic_type = 2 ) )
ORDER BY topic_id DESC
除了您的查询看起来正确之外,如果您无法使用它,我会尝试使用IN语句。
答案 1 :(得分:1)
我认为你的where子句是这样的:
WHERE ( (forum_id = '2' AND deleted = 0)
OR
(forum_id != '4' AND topic_type = '2' AND deleted = 0)
)
答案 2 :(得分:0)
试试这个:
SELECT topic_id, topic_title, topic_author, topic_type
FROM forum_topics
WHERE forum_id = 2
OR (forum_id != 4 AND topic_type = 2)
AND deleted = 0
ORDER BY topic_id DESC