cakephp 检索数据
cakephp Retrieving Data
我有 class 个,每个 class 个包含学生。
我想得到一个table包含每个class中的学生人数。
在这种情况下,如何找到:
$nums = $this->Student->find('count', array(
'group' => 'Student.Class_id'));
这个结果不正确
有几种存档方法:
使用带查找所有的虚拟字段
$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");
我有 class 个,每个 class 个包含学生。 我想得到一个table包含每个class中的学生人数。 在这种情况下,如何找到:
$nums = $this->Student->find('count', array( 'group' => 'Student.Class_id'));
这个结果不正确
有几种存档方法:
使用带查找所有的虚拟字段
$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");