【问题标题】:Counting results filtered by distinct() or group_by() in CodeigniterCodeigniter 中由 distinct() 或 group_by() 过滤的计数结果
【发布时间】:2013-04-29 01:05:42
【问题描述】:

我想用 CI 计算活动记录查询的结果(使用 postgreSQL)。我正在使用count_all_results(),它适用于大多数查询,但在连接后使用distinct()group_by() 时却不行,因为count_all_results() 忽略了这些功能。

这里有一个修复,它尚未在最新 (2.1.3) 稳定版本中实现。 https://github.com/EllisLab/CodeIgniter/commit/b05f506daba5dc954fc8bcae76b4a5f97a7433c1

当我尝试自己在当前版本中实施此修复时,没有完成额外的过滤。行数保持不变。

关于如何在当前版本中实现此功能的任何提示,或其他计算由distinct()group_by() 过滤的结果的方法?

【问题讨论】:

    标签: php database codeigniter postgresql


    【解决方案1】:
        $this->db->select('COUNT(id)');
        $this->db->join();
        $this->db->distinct();
        $this->db->group_by();
    //...etc ...
        $query = $this->db->get('mytable');
    
        return count($query->result()); 
    

        $this->db->join();
        $this->db->distinct();
        $this->db->group_by();
    //...etc ...
        $query = $this->db->get('mytable');
    
        return $query->num_rows(); 
    

    【讨论】:

    • 或返回 $query->num_rows()?
    【解决方案2】:

    你可以使用

    $this->db->group_by();
    

    否则

    $this->db->distinct();
    

    【讨论】:

      【解决方案3】:

      就我而言,从今天开始使用 Codeigniter 3.1.11,在执行 count_all_results() 时会考虑 distinct() 。但是,您必须使用该函数,执行 $this->db->select("distinct x") 然后计数,不会对记录计数应用 DISTINCT 限制。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 2015-05-26
        相关资源
        最近更新 更多