我遇到了一个问题。。。
我有一堆像这样的…
$this->db->where('Pool', "1");
$this->db->where('Bedrooms >=', "3");
然后是限额声明
$this->db->limit($limit, $offset);
最后我得到的声明
$query = $this->db->get('table-name');
我的问题是,我需要在limit语句之前计算结果,以获得没有limit的总行数。。所以我试了一下
$this->db->where('Pool', "1");
$this->db->where('Bedrooms >=', "3");
$num_rows = $this->db->count_all_results();
$this->db->limit($limit, $offset);
$query = $this->db->get('table-name');
这将使用where语句计算我的行数。。但是,get语句现在获取的记录没有以前的where语句。
它是不可见的,但有大量的代码处理更多的where语句,并在url中抓取东西,所以为了解决这个问题,我宁愿不执行两次数据检索。。。
干杯!
我知道这是一个老问题,但我只是遇到了这个问题,并想出了一个不同的解决方案。
这个想法是在限制和偏移之前复制db类。
$this->db->where('Pool', "1");
$this->db->where('Bedrooms >=', "3");
//here we use the clone command to create a shallow copy of the object
$tempdb = clone $this->db;
//now we run the count method on this copy
$num_rows = $tempdb->from('table-name')->count_all_results();
$this->db->limit($limit, $offset);
$query = $this->db->get('table-name');
我知道这是一个老问题,但我找到了一个非常简单的解决方案。
//do your select, from and where
$this->db->select('your selects');
$this->db->from('your table');
$this->db->where('your where');
//get the filtered rows count
//the trick is the first empty parameter and second false parameter
$filtered_count = $this->db->count_all_results('', false);
//limit your results and get the rows
$this->db->limit($length, $start);
$results = $this->db->get()->result_array();
希望它能帮助
$get_data = $this->your_model->get_data();
$data = $get_data['data'];
$count = $get_data['count'];
型号
function get_data($limit = 10, $offset= 0)
{
$table = 'table-name';
$where = array('Pool' => 1, 'Beedrooms >=' 3);
$return['data'] = $this->db->from($table)->where($where)->limit($limit, $offset)->get();
$return['count'] = $this->db->from($table)->where($where)->count_all_results();
return $return;
}
$this->db->select('*');
$this->db->from('users');
$this->db->where('active',$status);
//open1 here we copy $this->db in to tempdb and apply
//count_all_results() function on to this tempdb
$tempdb = clone $this->db;
$num_results= $tempdb->count_all_results();
// now applying limit and will get actual result
$this->db->limit(10);
$this->db->get();
$query = $this->db->last_query();
$res = $this->db->query($query);
$data_array = array('num_results' => $num_results, 'results' => $res->result() );
return $data_array;
很明显,您需要使用两个不同的查询。使用一个查询尽可能快地完成这项工作是最佳的,但由于您需要在第二个查询之前获得所有记录,因此我们需要使用两个查询。
但是,您可以根据MySQL使用的引擎来优化第一个查询。如果您使用InnoDB,那么您应该使用SELECT COUNT(*) FROM <table-name>
,因为总行大小缓存在InnoDB中。
我认为count_all_rows
使用count(*)
来提高性能,您应该直接使用它进行排序。
使用MyISAM,您可以使用COUNT(<column-name>)
。
因此,在模型类中有一个count函数,它返回表的计数,然后您可以调用该函数从数据库中插入/更新/获取数据。
您可以使用mysql 的SQL_CALC_FOUND_ROWS
SELECT SQL_CALC_FOUND_ROWS * FROM table1
WHERE
cond1, cond2, ..., condN
LIMIT 10
选择FOUND_ROWS();
这是使用mysql,您可能需要检查如何使用它的方式。
参考:
https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_found-行
我知道这个问题很老,但我遇到了同样的问题,得到了比这里显示的更简单的解决方案。不需要构建两次查询,也不需要克隆db class
。添加where
部分后,只需计算结果,而无需让查询生成器休息,然后添加limit
并执行查询。
$this->db->where('Pool', 1);
$this->db->where('Beedrooms >=' 3);
$count = $this->db->count_all_results('table-name', false); // No reset query builder
$this->db->limit($limit, $offset)
$result = $this->db->get();
您将有两个变量:CCD_ 9和CCD_。