为此,您可以使用 codeigniter 的子查询方式来执行此操作,您将不得不破解 codeigniter。像这样转到 system/database/DB_active_rec.php 从这些函数中删除 public 或 protected 关键字
public function _compile_select($select_override = FALSE)
public function _reset_select()
现在子查询写入可用现在这是您的带有活动记录的查询
$select = array('DISTINCT c2_id','f_id','f_name');
$this->db->select($select);
$this->db->from('file');
$this->db->order_by('f_id','DESC');
$subQuery1 = $this->db->_compile_select();
unset($select);
$this->db->_reset_select();
$select = array('DISTINCT c2_id','f_id','f2_name');
$this->db->select($select);
$this->db->from('file2');
$this->db->order_by('f2_id','DESC');
$subQuery2 = $this->db->_compile_select();
unset($select);
$this->db->_reset_select();
// And now your main query
$select = array(
'c1.c1_id',
'c1.c1_name',
'c2.c2_id',
'c2.c2_name',
'c2.c2_type',
'c2.c2_status',
'f.f_id',
'f.f_name',
'f2.f2_id',
'f2.f2_name'
);
$this->db->select($select);
$this->db->from('category2 c2');
$this->db->join("($subQuery1)",'f.c2_id = c2.c2_id','left');
$this->db->join("($subQuery2)",'f2.c2_id = c2.c2_id','left');
$this->db->where('c2.c2_status',1);
$this->db->group_by('c2.c2_id');
$main_query = $this->db->get();
事情就完成了。干杯!!!注意:在使用子查询时,您必须使用
$this->db->from('myTable')
代替
$this->db->get('myTable')
运行查询。
现在,您可以检查已构建为的查询
echo $this->db->last_query();