简体   繁体   English

如何在 codeigniter 中对 COUNT 查询执行 num_rows()?

[英]How to do a num_rows() on COUNT query in codeigniter?

This works:这有效:

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

But this doesn't:但这不是:

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

How to do a num_rows on a COUNT(*) query?如何对 COUNT(*) 查询执行 num_rows? Also is doing it the 2nd way any better performance wise?还是以第二种方式在性能方面有更好的表现?

Doing a COUNT(*) will only give you a singular row containing the number of rows and not the results themselves.执行COUNT(*)只会给您一个包含行数的单行,而不是结果本身。

To access COUNT(*) you would need to do要访问COUNT(*)你需要做

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

The second option performs much better since it does not need to return a dataset to PHP but instead just a count and therefore is much more optimized.第二个选项执行得更好,因为它不需要将数据集返回给 PHP,而只是一个计数,因此更加优化。

In CI it's really simple actually, all you need is在 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
$query->num_rows()

The number of rows returned by the query.查询返回的行数。 Note: In this example, $query is the variable that the query result object is assigned to:注意:在本例中,$query 是查询结果对象赋值给的变量:

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

echo $query->num_rows();

num_rows on your COUNT() query will literally ALWAYS be 1. It is an aggregate function without a GROUP BY clause, so all rows are grouped together into one. COUNT() 查询中的 num_rows 从字面上看总是 1。它是一个没有 GROUP BY 子句的聚合函数,因此所有行都被组合在一起。 If you want the value of the count, you should give it an identifier SELECT COUNT(*) as myCount... , then use your normal method of accessing a result (the first, only result) and get it's 'myCount' property.如果你想要计数的,你应该给它一个标识符SELECT COUNT(*) as myCount... ,然后使用你访问结果的常规方法(第一个,唯一的结果)并获得它的'myCount'属性。

As per CI Docs we can use the following,根据CI 文档,我们可以使用以下内容,

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

If we want to get total rows in the table without any condition, simple use如果我们想无条件获取表中的总行数,简单使用

echo $this->db->count_all_results('table_name'); // returns total_rows presented in the table
$query = $this->db->get();
if ($query->num_rows() > 0) {
   echo 'have row';
} else {
   echo 'no row return from db';
}

it's my way of solving the above given question这是我解决上述问题的方法

model模型

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

thanks谢谢

This will only return 1 row, because you're just selecting a COUNT() .这只会返回 1 行,因为您只是选择了一个COUNT() you will use mysql_num_rows() on the $query in this case.在这种情况下,您将在$query上使用mysql_num_rows()

If you want to get a count of each of the ID 's, add GROUP BY id to the end of the string.如果您想获得每个ID的计数,请将GROUP BY id添加到字符串的末尾。

Performance-wise, don't ever ever ever use * in your queries.在性能方面,永远不要在查询中使用* If there is 100 unique fields in a table and you want to get them all, you write out all 100, not * .如果一个表中有 100 个唯一字段并且您想要全部获取它们,您可以写出所有 100 个,而不是* This is because * has to recalculate how many fields it has to go, every single time it grabs a field, which takes a lot more time to call.这是因为*每次抓取一个字段时都必须重新计算它必须去多少个字段,这需要花费更多的时间来调用。

I'd suggest instead of doing another query with the same parameters just immediately running a SELECT FOUND_ROWS()我建议不要立即运行SELECT FOUND_ROWS()使用相同的参数进行另一个查询

    $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));   
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Codeigniter:当$ query-> num_rows()对我不起作用时,如何选择计数? - Codeigniter: how do I select count when `$query->num_rows()` doesn't work for me? 从函数中的查询中获取结果并在Codeigniter中计算num_rows - Getting result from a query in a function and count the num_rows in Codeigniter 删除查询代码时发生num_rows - num_rows on delete query codeigniter 如何正确检索num_rows | 笨 - How to properly retrieve num_rows | Codeigniter CodeIgniter num_rows()不起作用? - CodeIgniter num_rows() not working? 如何在codeigniter,jTable中仅显示过滤的记录计数(搜索返回的数据数num_rows()) - How to show only filtered record count (number of returned data from search, num_rows() ) in codeigniter, jTable Codeigniter query-> num_rows返回0不正确 - Codeigniter query->num_rows Returning 0 Incorrectly Codeigniter num_rows()返回0,同一查询返回1 - Codeigniter num_rows() returns 0, same query returns 1 Codeigniter如何将查询-> num_rows从模型发送到控制器 - codeigniter how to send query->num_rows from model to controler 如何使用嵌套 SELECT 获取 INSERT 查询的插入行数? 没有受影响的行,结果 ID,数量行 - How do I get a count on inserted rows for INSERT query with nested SELECT? No luck with affected_rows, result_id, num_rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM