The error is because you can't use an aggregate function (COUNT, MIN, MAX, AVG, etc) in the WHERE clause, without it being inside a subquery. Only the HAVING
clause allows you to use aggregates without being wrapped in subqueries.
But checking for replies to be more than zero is not necessary on an INNER JOIN - that guarantees that there will be at least one reply associated to the fb_user_pms
record. The JOIN also means that the information in t1
will be duplicated for every supported record in fb_user_pm_replies
. IE: If a fb_user_pms
record has three fb_user_pm_replies
records related to it, you'll see the fb_user_pms
record in the result set three times.
The query you want to use is:
SELECT t1.pm_id
FROM fb_user_pms AS t1
WHERE t1.pm_id = '{$pm_id}'
AND '{$username}' IN (t1.profile_author, t1.pm_author)
AND t1.deleted = 0
AND EXISTS(SELECT NULL
FROM fb_user_pm_replies AS t2
WHERE t2.pm_id = t1.pm_id
AND t2.pm_author = '{$username}')
The EXISTS clause returns true or false, based on the WHERE criteria. It also won't duplicate t1
results.