I currently have the following MySQL query, which executes fine without any errors:
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
However it's not doing what I intend it too, I want it to return all the results of topics WHERE the forum_id is 2 and deleted equals 0 aswell as return the results of topics where the forum_id does not equal 4 and the topic_type is 2 and deleted equals 0 (if they exist).
But currently its just doing the first just returning results of topics WHERE the forum_id is 2 and deleted equals 0 and not the other (even thoug开发者_StackOverflow中文版h they exist! :/).
I believe I'm doing something wrong...
All help is greatly appreciated.
I think your where clause would be something like this:
WHERE ( (forum_id = '2' AND deleted = 0)
OR
(forum_id != '4' AND topic_type = '2' AND deleted = 0)
)
To make it simpler for yourself, put the consistent part of your query (deleted=0) first:
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
Other than that your query looks right, I would try using an IN statement if you cant get it to work.
Try this:
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
精彩评论