【问题标题】:How to do a num_rows() on COUNT query in codeigniter?如何在codeigniter中对COUNT查询执行num_rows()?
【发布时间】:2012-07-06 11:24:58
【问题描述】:

这行得通:

        $sql = "SELECT id
                FROM `users`
                WHERE `account_status` = '" . $i . "'"; 
        $query = $this->db->query($sql);
        var_dump($query->num_rows());

但这不是:

        $sql = "SELECT COUNT(*)
                FROM `users`
                WHERE `account_status` = '" . $i . "'"; 
        $query = $this->db->query($sql);
        var_dump($query->num_rows());

如何对 COUNT(*) 查询执行 num_rows?第二种方式是否有更好的性能?

【问题讨论】:

  • 我还应该提到使用 CodeIgniter Active Record 类来帮助更好地组织代码。

标签: php mysql codeigniter


【解决方案1】:

这是我解决上述问题的方法

型号

$this->db->select('count(id) as ids');
$this->db->where('id', $id);
$this->db->from('your_table_name');

谢谢

【讨论】:

    【解决方案2】:

    根据CI Docs,我们可以使用以下内容,

    $this->db->where('account_status', $i); // OTHER CONDITIONS IF ANY
    $this->db->from('account_status'); //TABLE NAME
    echo $this->db->count_all_results();
    

    如果我们想在没有任何条件的情况下获取表中的总行数,简单使用

    echo $this->db->count_all_results('table_name'); // returns total_rows presented in the table
    

    【讨论】:

    • @danneth 早在 7 年前就已经提出了这个建议,这表明from() 调用是不必要的。
    • 你的意思是?文档说 yoir 第二个 sn-p 应该调用 count_all 而不是 count_all_results() - 但这不是 OP 所要求的。
    【解决方案3】:
        $list_data = $this->Estimate_items_model->get_details(array("estimate_id" => $id))->result();
        $result = array();
        $counter = 0;
        $templateProcessor->cloneRow('Title', count($list_data));
        foreach($list_data as $row) {
            $counter++;
            $templateProcessor->setValue('Title#'.$counter, $row->title);
            $templateProcessor->setValue('Description#'.$counter, $row->description);
            $type = $row->unit_type ? $row->unit_type : "";
            $templateProcessor->setValue('Quantity#'.$counter, to_decimal_format($row->quantity) . " " . $type);
            $templateProcessor->setValue('Rate#'.$counter, to_currency($row->rate, $row->currency_symbol));
            $templateProcessor->setValue('Total#'.$counter, to_currency($row->total, $row->currency_symbol));   
        }
    

    【讨论】:

    • 这与发布的问题一点也不相似。我认为这篇纯代码帖子是Not An Answer(并且无法挽救)。
    【解决方案4】:
    $query->num_rows()
    

    查询返回的行数。注意:在本例中,$query 是查询结果对象分配给的变量:

    $query = $this->db->query('SELECT * FROM my_table');
    
    echo $query->num_rows();
    

    【讨论】:

      【解决方案5】:

      在 CI 中其实很简单,你只需要

      $this->db->where('account_status', $i);
      $num_rows = $this->db->count_all_results('users');
      var_dump($num_rows); // prints the number of rows in table users with account status $i
      

      【讨论】:

        【解决方案6】:

        我建议不要立即运行SELECT FOUND_ROWS(),而不是使用相同的参数进行另一个查询

        【讨论】:

        • OP 没有进行两次连续查询,OP 提供了两种比较技术(作为努力证明)。
        【解决方案7】:

        您的 COUNT() 查询上的 num_rows 字面意思总是 1。它是一个没有 GROUP BY 子句的聚合函数,因此所有行都组合为一个。如果你想要计数的,你应该给它一个标识符SELECT COUNT(*) as myCount ...,然后使用你正常的方法来访问一个结果(第一个,唯一的结果)并得到它的'myCount'属性。

        【讨论】:

          【解决方案8】:

          这只会返回 1 行,因为您只是选择了 COUNT()。在这种情况下,您将在 $query 上使用 mysql_num_rows()

          如果您想获取每个ID 的计数,请将GROUP BY id 添加到字符串的末尾。

          就性能而言,永远不要在查询中使用*。如果一个表中有 100 个唯一字段,并且您想全部获取它们,则写出全部 100 个,而不是 *。这是因为* 每次抓取一个字段时都必须重新计算它必须去多少个字段,这需要更多的时间来调用。

          【讨论】:

          • OP 专门寻找一种 codeigniter 技术,而不是一般的mysql_ 建议。
          【解决方案9】:

          COUNT(*) 只会给你一个包含行数而不是结果本身的单数行。

          要访问COUNT(*),您需要这样做

          $result = $query->row_array();
          $count = $result['COUNT(*)'];
          

          第二个选项的性能要好得多,因为它不需要将数据集返回给 PHP,而只是一个计数,因此更加优化。

          【讨论】:

          • +1,但值得一提的是,您可以为列设置别名,然后使用它。喜欢SELECT COUNT(*) AS cnt ...,然后使用$result['cnt']
          • @Corbin 是的,我不得不这样做,COUNT(*) 因为密钥由于某种原因无法正常工作。
          • 跳过一步直接引用结果而不是先设置为数组:COUNT(*) AS cnt ... ,然后通过$query->row(0)->cnt访问
          • @Frug 对我不起作用,但使用 'c' 别名和 $query->row->c 确实
          猜你喜欢
          • 1970-01-01
          • 2016-09-27
          • 1970-01-01
          • 2013-05-02
          • 2017-09-27
          • 2018-04-30
          • 1970-01-01
          • 2018-08-14
          相关资源
          最近更新 更多