php - Codeigniter:将 activeRecord 与手动查询相结合?

标签 php mysql database activerecord codeigniter

我对 Codeigniter 中的 activerecord 与手动查询略有了解。 ActiveRecord 非常棒,因为它是关于标准查询的,而且开发时间非常短。

但是,当需要为查询添加一些复杂性时,ActiveRecord 的使用会变得相当复杂。子查询或复杂的连接至少让我很头疼。

由于当前的“$this->db->query”调用会立即执行设置的查询,因此它不能与正常的 activeRecord 调用结合使用。

那么,我能做些什么来结合这两种方法呢?

我想要完成的示例:

$this->db->select('title, content, date');
$this->db->from('mytable');
$this->db->manual('UNION'); // My own idea of db-call that appends UNION to the query
$this->db->select('title, content, date');
$this->db->from('mytable2');
$query = $this->db->get();

谢谢!

最佳答案

也许这个链接会有所帮助:active record subqueries

更新---

还有关于Union with Codeigniter Active Record的讨论.我同意那里的答案。

但是对于一些子查询,我们可以将事件记录与手动查询结合起来。示例:

// #1 SubQueries no.1 -------------------------------------------

$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
$subQuery1 = $this->db->_compile_select();

$this->db->_reset_select();

// #2 SubQueries no.2 -------------------------------------------

$this->db->select('title, content, date');
$this->db->from('mytable2');
$query = $this->db->get();
$subQuery2 = $this->db->_compile_select();

$this->db->_reset_select();

// #3 Union with Simple Manual Queries --------------------------

$this->db->query("select * from ($subQuery1 UNION $subQuery2) as unionTable");

// #3 (alternative) Union with another Active Record ------------

$this->db->from("($subQuery1 UNION $subQuery2)");
$this->db->get();

nb:抱歉,我还没有测试过这个脚本,希望它有用并且有用..

关于php - Codeigniter:将 activeRecord 与手动查询相结合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2985282/

相关文章:

java - 在数据库更新时使用 Spring 和 Hibernate 更新单例对象

javascript - 如何添加简单的图片上传功能?

php - 如何使用 GnuPG 加密字符串?

php - 有效地保存表中的数据,并且易于检索和回显

php - 将txt文件/数据导入到我的mysql数据库

php - 获取所有类别的帖子

mysql使用通配符从多个表中求和计数

sql - 尝试使用 PL/PgSQL 创建动态查询字符串以在 PostgreSQL 9.6 中创建 DRY 函数

php - 如何在从mysql检索到的图像上加水印

php - PHP读取CSV文件的问题