如何根据codeigniter

时间:2017-03-06 09:24:53

标签: php mysql codeigniter

我想以这样的方式编写查询:如果收到的id与$ id匹配,那么请求应该与profile表一起加入

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;
}

我试过这个,它显示以下错误

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

任何人都可以帮助我。谢谢。

2 个答案:

答案 0 :(得分: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 :(得分:0)

试试这个

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