简体   繁体   English

如何使用group by获取另一个表codeigniter中的字段计数?

[英]How to get count of a field in another table codeigniter using group by?

I have a table called sitekeys: 我有一个名为sitekeys的表:

sitekeys

In this site_key contains a unique license. 此site_key中包含唯一许可。 And The field called licenses contains the no of times it can be used. 并且称为许可证的字段包含可以使用的次数。 the org_id field contains the company to which it has been assigned. org_id字段包含已分配给该公司的公司。

Now the activation table: 现在激活表:

激活

in this table site_key contains the keys assigned to the device. 在此表中,site_key包含分配给设备的密钥。

Now I want to count The keys used for a company. 现在,我要计算用于公司的密钥。

Like for XYZ company i has give 10 licenes then i want to know how many of them are used by it, so for that i can count(site_key) in activation table. 像XYZ公司一样,我给了10个虱子,然后我想知道它使用了多少虱子,因此我可以在激活表中计数(site_key)。

So what should be my query here? 那么我在这里应该查询什么?

I think i wil have to use Group_BY to do so. 我认为我必须使用Group_BY来这样做。

I want show the keys i gave to a company and the keys they have used 我想显示我给一家公司的钥匙以及他们使用的钥匙

i don't know exactly the design of your table so safely i have to join the two table 我不太清楚您桌子的设计,所以我必须安全地将两张桌子合在一起

SELECT a.org_id, a.licenses, b.used_key from sitekeys a
LEFT JOIN 
(
 SELECT site_key, user_id, count(site_key) as used_key 
 FROM 
 activation b 
 GROUP BY site_key,user_id
) b
on a.site_key = b.site_key
$this->db->select("count(activation.site_key)");
$this->db->from("activation");
$this->db->join("sitekeys");
$this->db->group_by("sitekeys.site_key");
$this->db->get();

Try this 尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM