I am losing my hair, trying to understand what I am doing wrong, let me explain abit about my MySQL structure (so that you better understand) before I move on to the question.
I have a simple PHP forum and I have a column in both tables (for messages and topics) with the name "deleted" if it is 0, which means its display (considered not deleted / exists), or if it is 1 , it is hidden (considered remote / does not exist) - bool / lean.
Now the "specific criteria" that I'm talking about ... I want to get the total number of posts in a particular forum using its id (forum_id), ensuring that it only considers posts that are not deleted (deleted = 0), and their parent topics are not deleted (deleted = 0).
Column / table names are self-evident (see my efforts below for them, if necessary).
I tried the following (using a "simple" JOIN):
SELECT COUNT(t1.post_id) FROM forum_posts AS t1, forum_topics AS t2 WHERE t1.forum_id = '{$forum_id}' AND t1.deleted = 0 AND t1.topic_id = t2.topic_id AND t2.deleted = 0 LIMIT 1
I also tried this (using a subquery):
SELECT COUNT(t1.post_id) FROM forum_posts AS t1 WHERE t1.forum_id = '{$forum_id}' AND t1.deleted = 0 AND (SELECT deleted FROM forum_topics WHERE topic_id = t1.topic_id) = 0 LIMIT 1
But both of them do not meet specific criteria.
Appreciate all the help! :)
source share