我在使用 codeigniters 事件记录获取大型 WHERE
语句时遇到问题。
下面是我目前拥有的代码
public function get_pending_posts($userid){
$where = "posts.complete = 0 AND poststatuses.contact != 3 AND (poststatuses.poster = $userid OR poststatuses.accepter = $userid)";
$data = $this->db->select('posts.id AS postid, posts.option, a.gravatar, posts.title,p.firstname AS pfirstname, p.lastname AS plastname, a.firstname AS afirstname, a.lastname AS alastname, poststatuses.contact,poststatuses.modified')
->join('users p','poststatuses.poster = p.id')
->join('users a','poststatuses.accepter = a.id')
->join('posts','poststatuses.postid = posts.id')
->where($where)
->get('poststatuses');
$results = array();
if($data->num_rows()){
$results['num'] = $data->num_rows();
$results['requests'] = $data->result();
}
else{
$results['num'] = 0;
}
return $results;
}
当我调用此函数时,出现以下错误
Column 'complete' in where clause is ambiguous
SELECT * FROM (`poststatuses`) JOIN `posts` ON `posts`.`id` = `poststatuses`.`id` WHERE `complete` = 1 AND `rating` = 0
知道为什么 CI 将 posts.complete = 0
转换为 complete = 0
吗?
最佳答案
您可以将where()
方法的第三个参数设置为FALSE
,以防止CI向table
/field添加反引号
名称。
来自CI doc :
$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
如下:
$this->db->where($where, NULL, FALSE);
关于php - 使用 codeigniter 跨多个表进行复杂的 where/join 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21362011/