1
     $query=$this->db->select('*')
                     ->from('activation')
                     ->join('products','products.id_pro = activation.id_pro')
                     ->join('user','user.id = products.user_p')
                     ->order_by('id_key','DESC')
                     ->get();
     return $query->result();  

I have these code where I join result from 3 tables abd that works good. What I need more is counted rows from 4.table. That table is called license, and I need to count how many rows are with id_key (primary key for table activation). How to add that in my code?

1
  • You need to provide more information about tables Commented Apr 9, 2016 at 23:28

2 Answers 2

0

Try selecting license column as count

     $query=$this->db->select('*, COUNT(license.id_key ) as license_count')
                 ->from('activation')
                 ->join('products','products.id_pro = activation.id_pro')
                 ->join('user','user.id = products.user_p')
                 ->join('license','license.id_key = table.column?')
                 ->order_by('id_key','DESC')
                 ->get();

access the count within the object like: $query->result()->license_count;

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

3 Comments

I got this: Column 'id_key' in order clause is ambiguous
you need to do ->order_by('license.id_key','DESC') or the table name you want to order by then the column
It doesn't work properly, seen only one record from activation. Never mind, I solve on easier way with adding one more column in table.
0
     $query=$this->db->select('BaseTbl.*', 'Products.*', 'User.*', COUNT(License.id_key) as license_count)
                 ->from('activation as BaseTbl')
                 ->join('products as Products','Products.id_pro = BaseTbl.id_pro')
                 ->join('user as User','User.id = Products.user_p')
                 ->join('license as License','License.id_key = BaseTbl.id_key')
                 ->order_by('BaseTbl.id_key','DESC')
                 ->group_by('BaseTbl.id_key')
                 ->get();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.