你必須使用活動記錄類的_compile_select()
和_reset_select()
方法。
$this->db->select('*');
$this->db->from('chat');
$this->db->where("(userID='{$session}' AND toID='{$friendID}')");
$this->db->or_where("(userID='{$friendID}' AND toID='{$session}')");
$this->db->order_by('id', 'DESC');
$this->db->limit('10');
$subQuery = $this->db->_compile_select();
$this->db->_reset_select();
$this->db->select('*');
$this->db->from("{$subQuery} AS table");
$this->db->order_by('id', 'ASC');
$query = $this->db->get();
不幸的是,在CI 2.0+,_compile_select()
和_reset_select()
保護方法。遊民。你必須遵循this tutorial上擴展數據庫驅動程序,您可以編寫以下方法:
function get_compiled_select()
{
return $this->db->_compile_select();
}
function do_reset_select()
{
$this->db->_reset_select();
}
我想花時間指出,這種類型的行動將通過連接得到更好的服務。你應該考慮改變你的數據庫結構,以便聯接可能和有效。
是的。這是如何:http://codeigniter.com/user_guide/database/active_record.html – 2012-08-08 12:57:29