如何在codeigniter中加入3个表

时间:2013-07-11 08:52:54

标签: php codeigniter

我有3张桌子:

  

tbl_events - >主键= event_id

     

tbl_event_bids - > (event_id在其中没有主键)

     

tbl_users - >主键= u_id

我想获得这些字段:

  

tbl_events - > ADDED_DATE

     

tbl_events - > bid_end_date

     

tbl_events - > EVENT_DATE

     

tbl_event_bids - > BID_AMOUNT

     

tbl_users - > u_fname

     

tbl_users - > u_lname

没有任何错误,但数组为空。这段代码有错误吗?请帮忙......

public function get_confirmed_events($loged_user_id){

    $this->db->select(' tbl_events.event_name,
                        tbl_events.added_date,
                        tbl_events.bid_end_date,                            
                        tbl_events.event_date,

                        tbl_event_bids.bid_amount,

                        tbl_users.u_fname,
                        tbl_users.u_lname');

    $this->db->where('tbl_events.u_id', $loged_user_id);
    $this->db->where('tbl_events.closed_bid', 'N');

    $this->db->where('tbl_event_bids.bid_statues', 'W');        

    $this->db->join('tbl_event_bids', 'tbl_events.event_id = tbl_event_bids.event_id');
    $this->db->join('tbl_users', 'tbl_events.u_id = tbl_users.u_id');

    $data = $this->db->get('tbl_events');

    $confirmed_result = $data->result();

    return $confirmed_result;


}

2 个答案:

答案 0 :(得分:0)

试试这个

public function get_confirmed_events($loged_user_id){
$this->db->select(' tbl_events.event_name,
                    tbl_events.added_date,
                    tbl_events.bid_end_date,                            
                    tbl_events.event_date,
                    tbl_event_bids.bid_amount,
                    tbl_users.u_fname,
                    tbl_users.u_lname');      
$this->db->join('tbl_event_bids', 'tbl_events.event_id = tbl_event_bids.event_id','LEFT');
$this->db->join('tbl_users', 'tbl_events.u_id = tbl_users.u_id','LEFT');
$this->db->where('tbl_events.u_id', $loged_user_id);
$this->db->where('tbl_events.closed_bid', 'N');
$this->db->where('tbl_event_bids.bid_statues', 'W'); 
$data = $this->db->get('tbl_events');
$confirmed_result = $data->result();
return $confirmed_result;
}

我已使用LEFT JOIN如果想与其他人一起加入,您可以在join()的第三个参数中指定它,同时查看您的查询{my}}

答案 1 :(得分:0)

如果您正在使用联接,则必须使用FROM,用于主表

public function get_confirmed_events($loged_user_id){

$this->db->select(' tbl_events.event_name,
                    tbl_events.added_date,
                    tbl_events.bid_end_date,                            
                    tbl_events.event_date,

                    tbl_event_bids.bid_amount,

                    tbl_users.u_fname,
                    tbl_users.u_lname');
$this->db->from('tbl_events');
$this->db->where('tbl_events.u_id', $loged_user_id);
$this->db->where('tbl_events.closed_bid', 'N');

$this->db->where('tbl_event_bids.bid_statues', 'W');        

$this->db->join('tbl_event_bids', 'tbl_events.event_id = tbl_event_bids.event_id','left');
$this->db->join('tbl_users', 'tbl_events.u_id = tbl_users.u_id','left');

$data = $this->db->get();

$confirmed_result = $data->result();

return $confirmed_result;

}

我希望它会对你有所帮助。