[英]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.