问题描述
|
我目前有以下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,以及返回主题结果,其中forum_id不等于4并且topic_type为2,并且删除等于0(如果存在)。
但是目前,它只是在主题的第一个返回结果中进行操作,而forum_id为2且被删除的值等于0,而不是其他值(即使它们存在!:/)。
我相信我做错了什么...
非常感谢所有帮助。
解决方法
我认为您的where子句将是这样的:
WHERE ( (forum_id = \'2\' AND deleted = 0)
OR
(forum_id != \'4\' AND topic_type = \'2\' AND deleted = 0)
)
, 为了使自己更简单,请首先将查询的一致部分(已删除= 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语句。
, 尝试这个:
SELECT topic_id,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