1

How can i use group by for following case:

I have a table called Sitekey in here i have list of License keys.

Sitekey Table

The Site_Key field contains the license keys for my product. The Org_id contains the id of the organization to which that key has been assigned.

Now I have another table called activation. Its Structure is:

activation table

Now in this table Site_key contains the license(key). and the device information.

Now I want to show licenses available in table to whom they are assigned.

I do this :

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 ?

Note

I want to count the keys in activation table. For eg there is a key :4CAC-9CA7 Then i want to know how many times it has been used ? So i want to count all this for all keys

The Company Table:

company table

3
  • have you tried $this->db->group_by("columnName"); ?? Commented Feb 4, 2016 at 10:07
  • @AjayMakwana i saw that in user_guide. but i dont know how to use it here! what should be my query here Commented Feb 4, 2016 at 10:08
  • @MathBio i did not understant u please explain me Commented Feb 4, 2016 at 10:11

3 Answers 3

1
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; 

}
Sign up to request clarification or add additional context in comments.

8 Comments

no man, i dont need to use expiry data, i just want to show how many keys i have given to organization and how many have they used from it
Show structure "company"
added the company table , but i want to get the count of each key used in activation table,
So why you can't just GROUP BY site_key ?
SELECT company.org_name, sitekey.site_key, (SELECT COUNT(*) FROM activation WHERE user_id = company.id) FROM sitekey JOIN company ON sitekey.org_id = company.id It must return COMAPNY NAME, 4CAC-9CA7, 2 times ANOTHER COMPANY, 4CAC-9CA7, 12 times
|
0

Try this may be this will help you

public function get_key() {
               $result=$this->db->select('*')
                      ->from('activation')
                      ->group_by('yourfield')
                      ->get();
             $key_count= $query->num_rows();   
             return $result;
}

Comments

0

Sorry, I can't comment yet so I've to post an answer.

If you have a row per key in your activation table, you can just count everything

$this->db->from('activation')
->where('expiry_date <', date('U'))
->count_all_results();

will return an int

If you want to group by a column, it's easy, juste add a group_by

$this->db->from('activation')
->group_by('site_key')
->where('expiry_date <', date('U'))
->count_all_results();

EDIT:

If you want to know of many licenses their is left, you can try

$this->db->from('activation a')
->select('COUNT(a.id) as used, s.licenses as max, max - used as left')
->group_by('a.site_key')
->join('sitekey s', 'a.sitekey = s.site_key')
->get()->results();

3 Comments

i dont need expiry data here. i just want to count the licenses i gave them and the licenses they have used from it
in sitekey table the licenses field defines how many times they can use this key
@Rajan I edited my answer, can you tell me if the last part works for you ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.