使用codeigniter模型连接查询

9fkzdhlc  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(140)

请帮助我如何才能仅获得活动.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

dwbf0jvd

dwbf0jvd1#

请尝试以下操作:

$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。

**UPDATE:**我修复了在复制代码时错误编写get方法。

查看此处共享的查询片段:https://extendsclass.com/mysql/526c246
我希望能有所帮助

相关问题