there are two tables

blogi +----+-----------+ | id | blog_cate | +----+-----------+ | 1 | database | | 2 | database | | 4 | razrabotka| +----+-----------+ blog_cate +----+------------------+---------------+ | id | blogi_cate_title | blogi_cate_url| +----+------------------+---------------+ | 1 | Базы данных | database | | 2 | PHP | php | | 3 | Разработка | razrabotka | +----+----------+-----------------+------ 

It is necessary to make a sample

so it was

Databases 2

Development 1

PHP 0

 $this->db->select('blogi_cate.*, COUNT(blogi.blog_cate) as num'); $this->db->join('blogi', 'blogi.blog_cate = blogi_cate.blogi_cate_url'); $query = $this->db->get('blogi_cate'); 

Only one match does not work correctly.

 Array ( [0] => stdClass Object ( [blogi_cate_id] => 3 [blogi_cate_title] => Базы данных [blogi_cate_url] => database [num] => 15 ) ) 
  • it is not clear about the tables, you can imagine the structure, like an example here: + ---- + ---------- + ----------------- + ----------- + | id | username | email | password | + ---- + ---------- + ----------------- + ----------- + | 1 | Alex | alex@email.com | alexpass | | 2 | Max | max@email.com | maxpass | | 3 | Denis | denis@email.com | denispass | | 4 | Mark | mark@email.com | markpass | + ---- + ---------- + ----------------- + ----------- + - Smash
  • @ 2sldie in the question itself, format normally instead of the bar of what is. - Smash
  • Redid the structure. - 2sldie
  • like this: $ this-> db-> select ('blog_cate. *, COUNT (blogi.blog_cate) as num'); $ this-> db-> join ('blogi', 'blogi.blog_cate = blog_cate.blogi_cate_url'); $ this-> db-> group_by ('blogi.blog_cate'); $ query = $ this-> db-> get ('blog_cate'); - Smash
  • Thank you works) - 2sldie

1 answer 1

Reply from Smash comments

You need to add grouping by blogi.blog_cate

 <?php $this->db->select('blog_cate.*, COUNT(blogi.blog_cate) as num'); $this->db->join('blogi','blogi.blog_cate=blog_cate.blogi_cate_url'); $this->db->group_by('blogi.blog_cate'); $query = $this->db->get('blog_cate');