sql - multiple joins and group by in codeigniter -


i'm using codeigniter, want query returns users groups.

a group can have 1 or more users, here examples of tables:

groups table        users table              users_by_groups table     id                  id                    id_group   id_user     1                   10                      1          10     2                   11                      1          11     3                   12                      1          12                                                 2          11                                                 3          12 

the query made:

public function getusersbygroup(){         $this->db->select('g.vgro_name,u.vusr_user');         $this->db->from('vts_user_by_group b');         $this->db->join('vts_groups g', 'b.vubg_vgro_id = g.vgro_id');         $this->db->join('vts_users u', 'b.vubg_vusr_id = u.vusr_id');         $this->db->group_by('g.vgro_name,vusr_user');         // $this->db->order_by('u.vusr_user','asc');         $query = $this->db->get();         return $query->result();     } 

what returning:

      0 =>  object(stdclass)[28]   public 'vgro_name' => string 'group1' (length=4)   public 'vusr_user' => string 'john' (length=9) 1 =>  object(stdclass)[29]   public 'vgro_name' => string 'group1' (length=4)   public 'vusr_user' => string 'alice' (length=4) 2 =>  object(stdclass)[30]   public 'vgro_name' => string 'group2' (length=3)   public 'vusr_user' => string 'mark' (length=3) 

which not want. pretend example:

 0 =>  object(stdclass)[31]   public 'vgro_name' => string 'group1' (length=7)   public 'vusr_user' => string 'john' (length=7)   public 'vusr_user' => string 'alice' (length=7)      2 =>  object(stdclass)[30]   public 'vgro_name' => string 'group2' (length=3)   public 'vusr_user' => string 'mark' (length=3) 

what doing wrong?

you can 'grouping' in php:

public function getusersbygroup(){     $this->db->select('g.vgro_name,u.vusr_user');     $this->db->from('vts_user_by_group b');     $this->db->join('vts_groups g', 'b.vubg_vgro_id = g.vgro_id');     $this->db->join('vts_users u', 'b.vubg_vusr_id = u.vusr_id');     $query = $this->db->get();      $resultsbygroup = array();      foreach($query->result_array() $row) {         $resultsbygroup[$row['vgro_name']][] = $row['vusr_user'];     }      return $resultsbygroup; } 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -