Codeigniter count_all_results 然后得到 return 错误号 1066

Codeigniter count_all_results then get return error number 1066

我想在查询中使用 $this->db->count_all_results() 来计算所有结果,然后在不重置任何字段值的情况下获得查询结果 ($this->db->get)。我已经按照 Limiting or Counting Results 上的用户指南说

但是,此方法还会重置您可能已传递给 select() 的任何字段值。如果需要保留它们,可以将 FALSE 作为第二个参数传递:

我已将 FALSE 参数传递给该函数,但我收到数据库错误:

Error Number: 1066 Not unique table/alias: 'my_table'

这是我试过的代码

$this->db->select('title', 'content', 'date');
$this->db->like('title', 'Post');
$this->db->order_by('title', 'DESC');

$records = $this->db->count_all_results('my_table', FALSE);
$query = $this->db->get('my_table', 20);

谢谢

为什么不像这样计算查询结果集中的行数:

$this->db->select('title', 'content', 'date');
$this->db->like('title', 'Post');
$this->db->order_by('title', 'DESC');

$query = $this->db->get('my_table');

$records = $query->num_rows();

希望对您有所帮助:

count_all_results 中为 my_table 创建别名,如下所示:

$this->db->select('p.title, p.content, p.date');
$this->db->like('p.title', 'title');
$this->db->order_by('p.date', 'DESC');

$data['count'] = $this->db->count_all_results('my_table p', FALSE);
$data['records'] = $this->db->get('my_table')->result();
print_r($data);

更多:http://www.mysqltutorial.org/mysql-alias/