cakephp检索数据

时间:2015-04-08 20:00:49

标签: cakephp croogo

我有课,每节课都有学生。 我想得到一个表格,其中包含每个班级的学生人数。 在这种情况下如何找

$ nums = $ this-> Student-> find('count',array(           'group'=> 'Student.Class_id'));

结果不正确

1 个答案:

答案 0 :(得分:1)

有几种方法可以存档它:

  1. 将虚拟字段与find-all

    一起使用
    $this->Student->virtualFields = array('count' => 'COUNT(Student.' . $this->Student->primaryKey .')');
    $this->Student->find("all", array('fields' => 'count', 'group' => 'Student.Class_id'));
    
  2. 构建自定义查询

    $this->Student->query("SELECT COUNT(Student.{$this->Student->primaryKey}) as 'count' FROM students Student GROUP BY Student.Class_id");