我有两大类,我想取三个记录每个类别的下降每个类别的顺序后,我发现在这之后这个链接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