【问题标题】:codeigniter query builder database error where equal相等的codeigniter查询生成器数据库错误
【发布时间】:2016-07-25 21:07:27
【问题描述】:

我在将 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');

我认为问题在于 equals 分配,但你怎么看?我玩过它,但总是遇到同样的错误。

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    可能与 distinct() 有关。在 Codigniter 中有多种使用 distinct 的方法。

    $this->db->distinct();
    

    或者在你的例子中试试这个

    $this->db->select('DISTINCT(projects.id), projects.title');
    

    【讨论】:

    • 哦,好的。我没有意识到查询生成器有不同的方法。现在似乎可以正常工作了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多