我在将SQL语句转换为CodeIgniter查询构建器记录时遇到问题。在工作台中,我运行以下查询没有问题。
这适用于MySQL WorkBench ......
# This
SELECT DISTINCT
projects.id, projects.title
FROM
projects
INNER JOIN
positions
ON
(projects.id = positions.project_id)
WHERE (positions.is_draft = '0')
查询构建器
$this->db->select('DISTINCT projects.id, projects.title');
$this->db->from('projects');
$this->db->join('positions', 'projects.id = positions.project_id', 'inner');
$this->db->where('positions.is_draft', '0');
$query = $this->db->get();
return $query->result();
错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`id`, `projects`.`title` FROM `projects` INNER JOIN `positions` ON `projects`.`' at line 1
SELECT `DISTINCT` `projects`.`id`, `projects`.`title` FROM `projects` INNER JOIN `positions` ON `projects`.`id` = `positions`.`project_id` WHERE `positions`.`is_draft` =0
我也试过
$this->db->where('positions.is_draft = 0');
我认为问题在于平等分配,但你怎么看?我玩过它,但不断得到同样的错误。
答案 0 :(得分:1)
可能与 distinct()有关。在Codigniter中有几种使用distinct的方法。
$this->db->distinct();
或在您的示例中尝试此
$this->db->select('DISTINCT(projects.id), projects.title');