sql - MYSQL filter by count of another table -


i'm beggining mysql. have following query:

select answers.data. * , count( answers.reports.id ) rcount answers.data left join answers.reports on answers.data.id = answers.reports.answer_id answers.data.question='4' group answers.data.id 

it's returning me answers current question added column called rcount (number of reports)

i want return less 3 reports. tried following code:

select answers.data. * , count( answers.reports.id ) rcount answers.data left join answers.reports on answers.data.id = answers.reports.answer_id answers.data.question='4' , rcount < '3' group answers.data.id 

but aprrently that's not new condition goes. can't find in manual solution since i'm new this. thanks

use having clause:

select answers.data.* , count( answers.reports.id ) rcount answers.data left join answers.reports on answers.data.id = answers.reports.answer_id answers.data.question='4' group answers.data.id having count( answers.reports.id ) < 3 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -