你可以使用codeigniter的子查詢方式來做到這一點,你將不得不破解codeigniter。這樣 進入系統/數據庫/ DB_active_rec.php從這些功能
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();
查詢