php - 使用 codeigniter 跨多个表进行复杂的 where/join 语句

标签 php mysql codeigniter

我在使用 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/

相关文章:

php - 名称值对错误 "NameValuePair cannot be resolved to a type"

php - Zend 服务器与 xampp MySQL

jquery - 无法在 codeigniter View 中加载外部 javascript 文件

php - CodeIgniter:将数据从模型传递到 Controller

php - 使用下拉菜单和单选按钮来列出数据

php - 使用 php pdo 为一年中的每个日期添加一行

javascript - 如何在用户提交表单后显示成功消息

php - 如何将多个条目关联到另一个表中的另一个条目?

软删除前的MySQL检查

file - 在 codeigniter 中使用上传类 - 模型还是 Controller ?