【问题标题】:codeigniter active record get query and query without the LIMIT clausecodeigniter 活动记录获取查询和不带 LIMIT 子句的查询
【发布时间】:2011-11-17 07:46:00
【问题描述】:

我使用活动记录,一切正常,但我想将 $data["totalres"] 设置为总结果,我的意思是,相同的查询但没有 LIMIT

问题是,当您执行查询修饰符时,前面的语句会被取消设置,所以我什至无法在得到结果后添加 $this->db->limit()。

有什么想法吗?我认为只是为了这样做而“复制”查询是一种不好的做法

function get_search($start, $numrows, $filter = array())
{    

    ...

    $this->db
    ->select("emp")
    ->from('emp')
    ->join('empr', 'empr.b = empr.id', 'left')
    ->like('code', $code)
    ->limit($numrows, $start);

    ...

    $q = $this->db->get();        

    // number of rows WITHOUT the LIMIT X,Y filter
    $data["totalres"] = ???????;        

    if ($q->num_rows() > 0)
    {        
        $data["results"] = $q->result();
    } else {
        $data["results"] = array();
    }   

    return $data;
}    

【问题讨论】:

标签: php mysql codeigniter activerecord


【解决方案1】:

您可以使用SQL_CALC_FOUND_ROWS 来获取在没有-LIMIT 的情况下会返回的行数。注意select 行中的,FALSE。这告诉 CodeIgniter 不要试图用反引号转义 SELECT 子句(因为 SQL_CALC_FOUND_ROWS 不是一个字段,而 CodeIgniter 没有意识到这一点)。

$this->db
->select("SQL_CALC_FOUND_ROWS emp", FALSE)
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start);

$q = $this->db->get();

然后在运行该查询之后,我们需要运行另一个查询来获取总行数。

$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$data["totalres"] = $query->row()->Count;

【讨论】:

  • 谢谢..我认为 CI 会为此做好准备,但“普通”mysql 更好
  • 我认为他们没有内置,因为SQL_CALC_FOUND_ROWS 仅适用于 MySQL,而 CodeIgniter 应该适用于所有数据库类型。
  • 第二个 sn-p 中的第二行似乎不正确,它给出了错误。我建议: $query = $this->db->query('SELECT FOUND_ROWS() as Count')->row(); $result['size'] = $query->Count;
  • 这是一个很好的解决方案,但其中存在一个问题。如果您使用的是 MySQL DB Engine,那么它将完美运行,但如果您要更改任何其他数据库(如 Oracle),那么此解决方案不会工作。
【解决方案2】:

试试这个:

function get_search($start, $numrows, $filter = array()){    
    $tmp= $this->db
    ->select("emp")
    ->from('emp')
    ->join('empr', 'empr.b = empr.id', 'left')
    ->like('code', $code)
    ->_compile_select();

    $q= $this->db->limit($numrows, $start)->get();

    // number of rows WITHOUT the LIMIT X,Y filter

    $data["totalres"] = $this->db->query($tmp)->num_rows();

    if ($q->num_rows() > 0){        
        $data["results"] = $q->result();
    } else {
        $data["results"] = array();
    }   
    return $data;
}    

【讨论】:

【解决方案3】:

我实际上建议使用 CI 查询缓存。

要使用它,您需要启动缓存,构建没有限制的查询。运行查询以获取完整计数,然后应用限制并运行查询以获取具有所需偏移量的页面列表。

缓存会记住已定义的变量。

然后您可以为后续查询清除缓存。

例子

    $this->db->start_cache();

    // Where, like, having clauses in here

    $this->db->stop_cache();

    $count = $this->db->get('table')->num_rows();

    $this->db->limit('limit', 'offset');

    $result = $this->db->get('table')->result_array();

    $this->db->flush_cache();

【讨论】:

    【解决方案4】:
    $this->db
    ->select("SQL_CALC_FOUND_ROWS emp", FALSE)
    ->from('emp')
    ->join('empr', 'empr.b = empr.id', 'left')
    ->like('code', $code)
    ->limit($numrows, $start); $q = $this->db->get();
    
    $query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
    $data["totalres"] = $this->db->get()->row()->Count;
    

    CodeIgniter 2.1.0 没有运行,下面的代码会修复它。

    $query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
    $objCount = $query->result_array();
    $data["totalres"] = $objCount[0]['Count'];
    

    【讨论】:

    • 这个答案缺少教育解释。
    猜你喜欢
    • 2011-08-28
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多