使用 codeigniter 模型加入查询
Joining Query with codeigniter Model
请帮助我如何只在 activities.activity_id = response.activity_id 处得到响应?这是我的 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
试试这个:
$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;
您需要为表添加别名以构建更简化和有序的查询,在本例中别名是 a、b 和 c。
更新:我修复了复制代码时错误编写的 get 方法。
检查此处共享的查询片段:
https://extendsclass.com/mysql/526c246
希望对您有所帮助
请帮助我如何只在 activities.activity_id = response.activity_id 处得到响应?这是我的 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
试试这个:
$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;
您需要为表添加别名以构建更简化和有序的查询,在本例中别名是 a、b 和 c。
更新:我修复了复制代码时错误编写的 get 方法。
检查此处共享的查询片段: https://extendsclass.com/mysql/526c246
希望对您有所帮助