[英]codeigniter - MySQL - PHP Trying to count occurrences of different values
我有一个表Table:
这就是我要实现的目标。
我正在从控制器使用以下代码:
$data['total_rows'] = $this->MyModel->count_Names();
public function count_names()
{
$name1 = $this->db->get_where('Name_table',array('name'=>'John'));
return $name1 = $name1->num_rows();
}
上面的代码乏味且多余。 有什么建议可以使其干净。 只需一个代码即可单独找出所有出现的名称。 谢谢
活动记录样式:
$this->db->select('name, COUNT(*) as total');
$this->db->group_by('name');
$result = $this->db->get('Name_table');
$result_array = $result->result_array(); //Get the result as array
foreach($result_array as $name) {
echo $name['name'].' - '.$name['total'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.