![](/img/trans.png)
[英]how to count the number of rows returned by query in Codeigniter with Datamapper
[英]How to count number of rows while using UNION in codeigniter?
$this->db->select('*');
$this->db->from('table1');
$where = "tag = '".$tag."'";
$this->db->where($where);
$query1 = $this->db->get();
$result1 = $query1->num_rows();
$this->db->select('*');
$this->db->from('table2');
$where = "tag = '".$tag."'";
$this->db->where($where);
$query2 = $this->db->get();
$result2 = $query2->num_rows();
$result = $result1+$result2;
return $result;
在这个问题中,我有两个表table1 and table2
,其中两个表的结构相同。 现在,我要计算行数。 现在,它向我显示了错误的计数数据。 如果tag
数据存在于第一个表中,则其计数为1
;如果tag
数据存在于两个表中,则其计数为2
。 那么,我该怎么做? 请帮我。
谢谢
如果您的查询正常,并且每个查询都返回您需要的确切结果,那么现在您只想获取行数,那么最好使用count_all_results()
..并保持安全,并在进行另一个查询之前重置查询生成器:
...
$r1= $this->db->count_all_results();
$this->db->reset_query();
...
$r2= $this->db->count_all_results();
如果您的查询还可以,它们都应返回结果计数,然后只需将它们添加即可:
$sum = $r1 + $r2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.