Php Union Activerecord codeigniter框架
我正试图在codeigniter中执行联合。我在其他地方找到了这个代码框架:Php Union Activerecord codeigniter框架,php,sql,codeigniter,Php,Sql,Codeigniter,我正试图在codeigniter中执行联合。我在其他地方找到了这个代码框架: $this->db->select('title, content, date'); $this->db->from('mytable'); $query = $this->db->get(); $subQuery1 = $this->db->_compile_select(); $this->db->_reset_select(); // #
$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();
让我困惑的是,假设您在原始子查询中有一个get_where子句,您如何将其包括在内?大概是这样的:
问题1
$this->db->select('Id,title, content, date');
$this->db->from('mytable');
$query = $this->db->get_where('Boo', array('Id' => $foo['foo_Id']);
$subQuery1 = $this->db->_compile_select();
问题2
$this->db->select('title, content, date');
$this->db->from('mytable2');
$query = $this->db->get_where('Boo', array('Title' => $foo['foo_Title'])
$subQuery2 = $this->db->_compile_select();
谢谢。你可以把它们都写在一本书中。你不必那样做。我在这里写了一个例子:
$q = '
SELECT userid
FROM users
WHERE userid=10
UNION
SELECT userid
FROM users
WHERE userid=5
';
$result = $this->db->query($q);
return $result;
你发现的例子如果没有很好的记录,将很难遵循;我要提醒你不要这样做 您的其他选择包括:
- 观点
- 存储过程
- 硬编码查询
记住,我读到过这样做不是最好的方法这是一种简单明了的方法。你能分享你读到的吗?当然,在这里读:[link]是的,但如果你不是在处理一个大型项目,某一天你需要将整个数据库转换为Oracle、MSSQL或其他受支持的数据库系统,那么你可以使用这个解决方案。我目前正在使用MSSQL,问题是这样做,我的查询量会很大,因为我基本上是硬编码。