0

我正在尝试使用 codeigniter 框架运行查询。我想避免使用 WHERE 子句,而是将条件作为 ON 子句的一部分包含在查询的 JOIN 语句中。由于 JOIN 语句在 WHERE 子句之前执行,因此这将更加有效。这是我的代码:

    $this->db->select('SQL_CALC_FOUND_ROWS
        projects.id, projects.project_name, projects.date_modified, 
        projects.last_modifier, projects.status, project_users.permission', false)->
            from('projects');
        $this->db->join('project_users', 'project_users.id = projects.id AND 
            project_users.user_id = ' . $this->user_id);
    if ($orderby !== false) {
        $this->db->order_by($orderby, $direction);
    }
    if ($limit !== null) {
        $this->db->limit($limit, $offset);
    }
    $query = $this->db->get();

当我尝试加载页面时,我收到一个 SQL 错误。似乎codeigniter不包括AND之后的条件:

SELECT SQL_CALC_FOUND_ROWS projects.id, projects.project_name, projects.date_modified, projects.last_modifier, projects.status, project_users.permission FROM (`projects`) JOIN `project_users` ON `project_users`.`id` = `projects`.`id` AND ORDER BY `project_name` ASC LIMIT 5, 0
4

1 回答 1

0
join('project_users', 'project_users.id = projects.id AND project_users.user_id = ' . $this->user_id);

应该

$this->db->join('project_users', 'project_users.id = projects.id AND 
        project_users.user_id = ' . $this->user_id);
于 2013-08-19T17:00:08.750 回答