php - 如何在 codeigniter 中使用 group by 来计算一些值?

标签 php mysql codeigniter

我如何在以下情况下使用分组依据:

我有一个名为 Sitekey 的表,这里有许可证 key 列表。

Sitekey Table

Site_Key 字段 包含我的产品的许可证 key 。 Org_id 包含已分配该 key 的组织的 ID。

现在我有另一个名为activation 的表。 它的结构是:

activation table

现在在此表中,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 那么我想知道它被使用了多少次?所以我想为所有键计算所有这些

公司表:

company table

最佳答案

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/

相关文章:

codeigniter - fsockopen() : unable to connect to ssl://smtp. gmail.com:465(权限被拒绝)

php - 无法显示数据结果codeigniter

php - 在 Paypal ( Paypal 订阅)中首次定期付款时应用折扣优惠券代码

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

PHP : Better date parser than strtotime

mysql - Rails sql查询中动态传递参数

php - 无法更新 php 和 MySQL 中的文件名

php - 创建动态链接而不创建新的 .php 页面/文件

mysql - 不能有 "nonidentifying"N :M relationship?

php - CodeIgniter:从表单数组获取数据到帖子数组