我有课,每节课都有学生。 我想得到一个表格,其中包含每个班级的学生人数。 在这种情况下如何找:
$ nums = $ this-> Student-> find('count',array( 'group'=> 'Student.Class_id'));
结果不正确
答案 0 :(得分:1)
有几种方法可以存档它:
将虚拟字段与find-all
一起使用$this->Student->virtualFields = array('count' => 'COUNT(Student.' . $this->Student->primaryKey .')');
$this->Student->find("all", array('fields' => 'count', 'group' => 'Student.Class_id'));
构建自定义查询
$this->Student->query("SELECT COUNT(Student.{$this->Student->primaryKey}) as 'count' FROM students Student GROUP BY Student.Class_id");