我有两个模型,PosthasMany Comment。如何选择所有Post少于两个的Comment?
我尝试使用 a findwith 'fields'=>array('COUNT(Comment.id) as numComments','Post.*'),(然后做 a numComments < 2in 'conditions')。但是,我得到一个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
#1111 - Invalid use of group function但是我在COUNT功能上遇到错误。
编辑:已解决,使用 HAVING COUNT 而不是 WHERE COUNT。