1
SELECT jos_mod_gdwcomm.*, COUNT(jos_mod_gdwcomm_spam.reportid) `num_spam` 
FROM `jos_mod_gdwcomm` 
WHERE `num_spam` > 0 
LEFT JOIN `jos_mod_gdwcomm_spam` 
ON (jos_mod_gdwcomm.id = jos_mod_gdwcomm_spam.commid) 
GROUP BY jos_mod_gdwcomm.id
ORDER BY `num_spam` DESC, jos_mod_gdwcomm_spam.datetime DESC

How can i modify this query so it only returns the rows that num_spam is greater than 0.

i tryed putting

WHERE `num_spam` > 0

In a few places but no luck.

Thanks

3 Answers 3

2

You need a HAVING clause:

SELECT jos_mod_gdwcomm.*, COUNT(jos_mod_gdwcomm_spam.reportid) `num_spam` 
FROM `jos_mod_gdwcomm` 
LEFT JOIN `jos_mod_gdwcomm_spam` 
ON (jos_mod_gdwcomm.id = jos_mod_gdwcomm_spam.commid) 
GROUP BY jos_mod_gdwcomm.id

HAVING num_spam > 0

ORDER BY `num_spam` DESC, jos_mod_gdwcomm_spam.datetime DESC
Sign up to request clarification or add additional context in comments.

Comments

2

Try with GROUP BY jos_mod_gdwcomm.id HAVING num_spam > 0.

1 Comment

Thanks it works ill approuve your answer in a few mins... wont let me just yet
1

When you want to test a condition on an aggregate function, you use a HAVING clause.

SELECT jos_mod_gdwcomm.*, COUNT(jos_mod_gdwcomm_spam.reportid) `num_spam` 
    FROM `jos_mod_gdwcomm` \
        LEFT JOIN `jos_mod_gdwcomm_spam` 
            ON (jos_mod_gdwcomm.id = jos_mod_gdwcomm_spam.commid) 
    GROUP BY jos_mod_gdwcomm.id
    HAVING COUNT(jos_mod_gdwcomm_spam.reportid) > 0 
    ORDER BY `num_spam` DESC, jos_mod_gdwcomm_spam.datetime DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.