Home > Mobile >  Joining Query with codeigniter Model
Joining Query with codeigniter Model

Time:02-28

Please help me how can I get only response where activities.activity_id = response.activity_id? here is my CI_model

public function get_response(){
   
    $this->db->select('*');
    $this->db->from('response');
     $id = $this->session->userdata('id');
   $this->db->where_in('response.user_id', $id);
   $this->db->join('activities', 'response.activity_id = activities.activity_id');
     $this->db->join('users', 'users.id = response.user_id');
     
    $result = $this->db->get()->result_array();
    return $result;
}

My Activities table

My Response Table

My users table

CodePudding user response:

Try with this:

$id = $this->session->userdata('id');

$this->db->select('a.*, b.*, c.*');
$this->db->join('activities b', 'a.activity_id = b.activity_id');
$this->db->join('users c', 'c.id = a.user_id');
$this->db->where_in('a.user_id', $id);

$result = $this->db->get('response a')->result_array();

return $result;

You need to add aliases to the tables in order to build a more simplified and ordered query, in this case the aliases are a, b and c.

UPDATE: I fix a get method incorrectly writed when copy your code.

Check the query snippet shared here: https://extendsclass.com/mysql/526c246

I hope to be helpful

  • Related