我有兩大類,我想取三個記錄每個類別的下降每個類別的順序後,我發現在這之後這個鏈接UNION query with codeigniter's active record pattern我改變我的DB_Active_rec文件,並添加以下代碼也獲得三個記錄使用笨
var $unions = array();
public function union_push($table = '') {
if ($table != '') {
$this->_track_aliases($table);
$this->from($table);
}
$sql = $this->_compile_select();
array_push($this->unions, $sql);
$this->_reset_select();
}
public function union_flush() {
$this->unions = array();
}
public function union() {
$sql = '(' . implode(') union (', $this->unions) . ')';
$result = $this->query($sql);
$this->union_flush();
return $result;
}
public function union_all() {
$sql = '(' . implode(') union all (', $this->unions) . ')';
$result = $this->query($sql);
$this->union_flush();
return $result;
}
,然後我創建這樣
$this->db->select("*");
$this->db->from("media m");
$this->db->join("category c", "m.category_id=c.id", "INNER");
$this->db->order_by("m.media_files", "DESC");
$this->db->limit(3);
$this->db->union_push();
$this->db->select("*");
$this->db->from("media m");
$this->db->join("category c", "m.category_id=c.id", "INNER");
$this->db->order_by("m.media_files", "DESC");
$this->db->limit(3);
$this->db->union_push();
$getMedia = $this->db->union_all();
CodeIgniter的基於函數查詢創建此
(SELECT * FROM media m INNER JOIN category c ON
m.category_id = c.id ORDER BY m.media_files DESC LIMIT 3)
UNION ALL
(SELECT * FROM media m INNER JOIN category c ON
m.category_id = c.id ORDER BY m.media_files DESC LIMIT 3)
現在它是提取記錄,但不正確我想只使用查詢,它顯示六個記錄第一個查詢獲取3個記錄和第二個查詢獲取三個記錄現在記錄是重複的我檢查記錄的id是6,5,4並再次6,5,4。它也可以通過PHP
完成,但我想使用查詢。在此先感謝
其實我使用codeigniter的內置函數,如$ this-> db-> select('*');和其他功能,所以我如何實現它。 – user3833682 2014-09-03 14:23:28
有什麼辦法可以實現它嗎? – user3833682 2014-09-04 10:12:09
先生'GolezTrol'現在在編輯後看到我的問題。 – user3833682 2014-09-04 11:27:19