【发布时间】:2010-06-26 08:06:20
【问题描述】:
我有两个模型,Post hasMany Comment。如何选择少于两个Comment 的所有Post?
我尝试使用find 和'fields'=>array('COUNT(Comment.id) as numComments','Post.*'),(然后在'conditions' 中使用numComments < 2)。但是,我收到 Unknown column 'Comment.id' in 'field list' 错误。
谢谢!
编辑:我已经让 CakePHP 生成这个查询:
SELECT `Post`.*, FROM `posts` AS `Post`
LEFT JOIN comments AS `Comment` ON (`Post`.`id` = `Comment`.`text_request_id`)
WHERE COUNT(`Comment`.`id`) < 2
GROUP BY `Comment`.`post_id`
LIMIT 10
但我在COUNT 函数上收到错误#1111 - Invalid use of group function。
编辑: 已解决,使用 HAVING COUNT 而不是 WHERE COUNT。
【问题讨论】:
-
是的,对不起,我的 SQL 有点生疏了,我正要建议
HAVING。 :)
标签: mysql cakephp cakephp-1.3 mysql-error-1111