2017-03-06 106 views
0

表我想以這樣的方式,如果接收到的ID與$ ID匹配,那麼請求應該與配置表合併爲如何加入基於條件笨

r.sent_id = profile_id 

編寫一個查詢如果有sent_id比賽$ ID,然後

r.sent_id = profile_id 

我的模型,

public function getRequestedDetails($id) { 
$this->db->select('*'); 
$this->db->from('profile'); 
$stat = $this->db->where('recieved_id', $id); 
if($stat) 
$this->db->join('requests enter code hereas r', 'r.sent_id = profile_id', 'left'); 
$stat = $this->db->where('sent_id', $id); 
if($stat) 
$this->db->join('requests as r', 'r.recieved_id = profile_id', 'left'); 
$this->db->where('r.status', 1); 
$query = $this->db->get(); 
$list = $query->result(); 
return $list; 
} 

我嘗試這樣做,它顯示了以下埃羅[R

Error Number: 1066 

不是唯一的表/別名: 'R'

SELECT * FROM `profile` LEFT JOIN `requests` as `r` ON `r`.`sent_id` = `profile_id` LEFT JOIN `requests` as `r` ON `r`.`recieved_id` = `profile_id` WHERE `recieved_id` = '5' AND `sent_id` = '5' AND `sent_id` = '5' AND `r`.`status` = 1 

誰能幫助me.Thanks。

+0

沒有用添加該'$ STAT = $這個 - > DB->的,其中( 'recieved_id',$ ID);'和'$ stat = $ this-> db-> where('sent_id',$ id);'if id isset its all set,if null null –

+0

as wll there is separate id too'$ this-> db->在哪裏('sent_id',$ id);' –

回答

1

嘗試此..

public function getRequestedDetails($id) { 
    $query = $this->db->select('*') 
      ->from('profile') 
      ->join('requests','(requests.sent_id = ' . $id . ' AND requests.sent_id = profile.profile_id) 
       OR (requests.recieved_id = ' . $id . ' AND requests.recieved_id = profile.profile_id'), 'left') 
      ->where('profile.status',1) 
      ->get(); 

    $list = $query->result(); 
    return $list; 
} 
1

嘗試此

SELECT * 
FROM profile 
     LEFT JOIN requests AS requests 
       ON (requests.sent_id = profile.profile_id OR requests.recieved_id = profile.profile_id) 
WHERE profile.recieved_id = '5' 
     AND profile.sent_id = '5' 
     AND profile.sent_id = '5' 
     AND requests.status = 1 
+0

嗨Rushil,你可以用codeigniter – Daniel