Heya!, I have the below query:
SELECT t1.pm_id
FROM fb_user_pms AS t1,
fb_user_pm_replies AS t2
WHERE (t1.pm_id = '{$pm_id}'
AND t1.profile_author = '{$username}'
OR t1.pm_author = '{$username}'
AND t1.pm_id = t2.pm_id
AND t2.pm_author = '{$username}'
AND COUNT(t2.reply_id) > 0)
AND t1.deleted = 0
However, I’m getting a grouping error – my guess is its caused by the AND COUNT(t2.reply_id) > 0?
How can I rectify the above query to make it work.
Hope someone can help.
Cheers!
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
HAVINGclause 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_pmsrecord. The JOIN also means that the information int1will be duplicated for every supported record infb_user_pm_replies. IE: If afb_user_pmsrecord has threefb_user_pm_repliesrecords related to it, you’ll see thefb_user_pmsrecord in the result set three times.The query you want to use is:
The EXISTS clause returns true or false, based on the WHERE criteria. It also won’t duplicate
t1results.