MYSQL按另一个表的计数过滤
我从 MYSQL 开始。 我有以下查询:
SELECT answers.data. * , COUNT( answers.reports.id ) AS rcount
FROM answers.data
LEFT JOIN answers.reports ON answers.data.id = answers.reports.answer_id
WHERE answers.data.question='4'
GROUP BY answers.data.id
它返回当前问题的所有答案,并添加一个名为 rcount(报告数量)的列,
我希望它仅返回报告数量少于 3 个的列。我尝试了以下代码:
SELECT answers.data. * , COUNT( answers.reports.id ) AS rcount
FROM answers.data
LEFT JOIN answers.reports ON answers.data.id = answers.reports.answer_id
WHERE answers.data.question='4' AND rcount < '3'
GROUP BY answers.data.id
但显然这不是新条件所在的位置。我在手册中找不到解决方案,因为我对此很陌生。 谢谢
I'm beggining with MYSQL.
I have the following query:
SELECT answers.data. * , COUNT( answers.reports.id ) AS rcount
FROM answers.data
LEFT JOIN answers.reports ON answers.data.id = answers.reports.answer_id
WHERE answers.data.question='4'
GROUP BY answers.data.id
It's returning me all answers to the current question with an added column called rcount (number of reports)
I want it to return only those with less than 3 reports. I tried the following code:
SELECT answers.data. * , COUNT( answers.reports.id ) AS rcount
FROM answers.data
LEFT JOIN answers.reports ON answers.data.id = answers.reports.answer_id
WHERE answers.data.question='4' AND rcount < '3'
GROUP BY answers.data.id
but aprrently that's not where the new condition goes. I can't find in the manual a solution since I'm new to this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
HAVING
子句:Use a
HAVING
clause: