我对 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/