我有一张 table 表:
- 姓名
- 1 约翰
- 2 詹姆斯
- 约翰三世
- 4 詹姆斯
- 5 玛丽
这就是我想要达到的目标。
- 点名
- 约翰福音 2
- 詹姆斯 2
- 玛丽 1
我正在使用来自 Controller 的这段代码:
$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'];
}
关于php - codeigniter - MySQL - PHP 试图计算不同值的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22847533/