Codeigniter db_where在3个连接表中

时间:2017-07-06 19:33:46

标签: codeigniter filter

所以这段代码是3个表连接的组合,现在我想过滤查询,其中只有用户可以拥有的请求是他/她自己的请求而不是来自其他用户..问题出在where子句i有重复。

$this->db->order_by('loanrequest.ApplicationNo', 'DESC');
        $query = $this->db->get('loanrequest','loanapplication','user');
        $this->db->join('loanapplication','loanrequest.ApplicationNo = loanapplication.ApplicationNo');
        $this->db->join('user', 'user.userId = loanapplication.userId');    
        $this->db->where('loanapplication.userId', $this->session->userdata('userId'));
        $this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
        return $query->result_array();

2 个答案:

答案 0 :(得分:0)

我无法获得您在连接查询中所做的操作,但以下是我如何加入我的CodeIgniter模型。

您无需将其置于条件:

$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');

因为您已在连接查询中完成了此操作。

$query = $this->db->select('*')
            ->from('loanapplication')
            ->join('loanrequest', 'loanrequest.ApplicationNo  = loanapplication.ApplicationNo', 'left')
            ->join('user', 'user.userId  = loanapplication.userId', 'left')     
            ->where_in('loanapplication.userId',$this->session->userdata('userId'))
            ->order_by('loanrequest.ApplicationNo', 'DESC')                 
            ->get();

return $query->result_array();

我建议您使用$this->db->last_query(),因为它可以让您更好地了解您的查询。

答案 1 :(得分:0)

$query = $this->db->select('*')
            ->from('loanapplication')
            ->join('loanrequest', 'loanrequest.ApplicationNo  = loanapplication.ApplicationNo', 'left')
            ->join('user', 'user.userId  = loanapplication.userId', 'left')     
            ->where_in('loanapplication.userId',$this->session->userdata('userId'))
            ->order_by('loanrequest.ApplicationNo', 'DESC');                    
            $result = $query->get();

           echo $this->db->last_query();exit; //use this to print your query so that you can get the actual issue 
           if ($result->num_rows() > 0) {
            return $result->result_array();
          } else
            return '';