我如何在以下情况下使用分组依据:
我有一个名为 Sitekey 的表,这里有许可证 key 列表。
Site_Key 字段 包含我的产品的许可证 key 。 Org_id 包含已分配该 key 的组织的 ID。
现在我有另一个名为activation 的表。 它的结构是:
现在在此表中,Site_key 包含许可证( key )。和设备信息。
现在我想在表格中显示可用的许可证分配给谁。
我这样做:
public function get_key()
{
$this->db->select('*');
$this->db->from('sitekey');
$this->db->join('company', 'sitekey.org_id = company.id');
$this->db->order_by('sitekey.id','desc');
$query = $this->db->get();
$result = $query->result();
return $result;
}
Now I want get the count of keys that are used in activation table! > in sitekey table the licenses field contains the number of licenses so the company can use that key for that number of times. Now i want to get the key i gave them and the keys they have used from it. So i guess i need to use group by So how can i write such a query ?
注意
我想计算激活表中的键数。例如有一个键:4CAC-9CA7
那么我想知道它被使用了多少次?所以我想为所有键计算所有这些
公司表:
最佳答案
public function get_key()
{
$this->db->select('company.org_name, sitekey.site_key, (SELECT COUNT(*) FROM activation WHERE activation.site_key= sitekey.site_key) as key_count');
$this->db->from('sitekey');
$this->db->join('company', 'sitekey.org_id = company.id');
$query = $this->db->get();
$result = $query->result();
var_dump($result);
return $result;
}
关于php - 如何在 codeigniter 中使用 group by 来计算一些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35197712/