我在数据库中有一个表:
id | int(25) Auto Increment
game | int(10)
user | int(9)
own | int(11)
userrate| int(2)
time | datetime
我现在想要的是对所有自己的类型的行进行计数.我知道这很难理解,因此我将举一个例子:
- own: 1; count: 5;
- own: 2; count: 3;
- own: 4; count: 10;
等等
我最初的想法是为每个查询创建不同的查询(因为只有四个查询),如下所示:
$this->db->where('user',$user);
$this->db->from('ownership');
$this->db->where('own','1');
$whole = $this->db->count_all_results();
return $whole;
但是我认为那根本不好.你能建议我正确的方法吗?
解决方法:
只是将结果分组:
$res = $this->db->query("SELECT count(1) AS games,own FROM ownership WHERE user = ? GROUP BY own", array($user))->result_array();