查询生成器 Codeigniter 3

Posted

技术标签:

【中文标题】查询生成器 Codeigniter 3【英文标题】:Query Builder Codeigniter 3 【发布时间】:2016-07-05 00:54:33 【问题描述】:

我这个mysql查询

SELECT project.title project_title, category.category, project_details.description description, SUM(project_details.quantity) total_quantity, project_details.unit, SUM(project_details.price) total_price, SUM(project_details.jan) total_jan, SUM(project_details.feb) total_feb, SUM(project_details.mar) total_mar, SUM(project_details.apr) total_apr, SUM(project_details.may) total_may, SUM(project_details.jun) total_jun, SUM(project_details.jul) total_jul, SUM(project_details.aug) total_aug, SUM(project_details.sep) total_sep, SUM(project_details.oct) total_oct, SUM(project_details.nov) total_nov, SUM(project_details.dec) total_dec
FROM project_details
JOIN project ON project_details.project_id = project.id
JOIN category ON project_details.category_id = category.id
JOIN user ON project.user_id = user.id
RIGHT JOIN office ON user.office_id = office.id
WHERE EXISTS 
    (
        SELECT t1.id id1, t2.id id2, t3.id id3, t4.id id4, t5.id id5
        FROM user t1
        LEFT JOIN user t2 ON t2.reports_to = t1.id
        LEFT JOIN user t3 ON t3.reports_to = t2.id
        LEFT JOIN user t4 ON t4.reports_to = t3.id
        LEFT JOIN user t5 ON t5.reports_to = t4.id
        WHERE t1.id = 81
    )
    AND project.approval_status = 22 AND project.reason_for_rejection IS NULL
GROUP BY project.id, project_details.description
ORDER BY project_details.category_id

但是我在 Codeigniter 3.0 中找不到任何可以找到 WHERE EXISTS 的文档。我已经检查过这个https://www.codeigniter.com/userguide3/database/query_builder.html 但没有。

这是我现在的代码

$this->db->select('project.title project_title, category.category, project_details.description description, SUM(project_details.quantity) total_quantity, project_details.unit, SUM(project_details.price) total_price, SUM(project_details.jan) total_jan, SUM(project_details.feb) total_feb, SUM(project_details.mar) total_mar, SUM(project_details.apr) total_apr, SUM(project_details.may) total_may, SUM(project_details.jun) total_jun, SUM(project_details.jul) total_jul, SUM(project_details.aug) total_aug, SUM(project_details.sep) total_sep, SUM(project_details.oct) total_oct, SUM(project_details.nov) total_nov, SUM(project_details.dec) total_dec');
$this->db->from('project_details');
$this->db->join('project', 'project_details.project_id = project.id');
$this->db->join('category', 'project_details.category_id = category.id');
$this->db->join('user', 'project.user_id = user.id');
$this->db->join('office', 'user.office_id = office.id', 'right');
// THIS IS WHERE I WANT TO PUT THE WHERE EXISTS
$this->db->where('project.approval_status', $user_id);
$this->db->where('project.reason_for_rejection IS NULL', NULL, TRUE);
$this->db->group_by('project.id');
$this->db->group_by('project_details.description');
$this->db->order_by('project_details.category_id');
$query = $this->db->get();
return $query->result();

有人可以帮助我如何在 codeigniter 中实现这一点吗?高级感谢。

【问题讨论】:

你试过$query = $this->db->query('your large query');吗? $this->db->query('query') 不是很容易被 SQL 注入吗? 你应该阅读***.com/questions/1615792/… 感谢您的回答。我会用这个。 【参考方案1】:

当您有一个非常大的查询,或者为您的目的而制作的查询构建器中没有内置函数时,您可以随时使用:

$query = $this->db->query('your large query');

您还应该注意注射,因此您还应该阅读此post

这很好地解释了如何使用$this->db->query 方法对抗 sql 注入。

希望对你有帮助

【讨论】:

以上是关于查询生成器 Codeigniter 3的主要内容,如果未能解决你的问题,请参考以下文章

codeigniter 查询生成器和活动记录 sql 注入

codeigniter 活动记录,生成,但不执行查询

如何在 CodeIgniter 中为 is_unique 表单验证启用查询生成器?

或在我的 SQL 查询(使用 CodeIgniter 生成)中不使用 jQuery DataTables

Codeigniter 将查询组合在一个循环中

在codeigniter查询生成器中使用union,并在虚拟mysql列中进行过滤