简体   繁体   English

计算 Codeigniters 中两个 MySQL 表中的所有行

[英]Count all rows from two MySQL tables in Codeigniters

Hi need to count all rows in two MySQL tables.您好需要计算两个 MySQL 表中的所有行。 Tables have same structure and the code I'm using in the model file is the following:表具有相同的结构,我在 model 文件中使用的代码如下:

$this->regs_db->select ("*");
$this->regs_db->from("$this->table_images_civil, $this->table_images_military");

return $this->regs_db->count_all_results();

The result I'm getting is not correct at all.我得到的结果根本不正确。 I know that in the first table there are 10,934 rows, in the second one there are 1,299 rows...but the result I get is 14,203,266.我知道在第一个表中有 10,934 行,在第二个表中有 1,299 行......但我得到的结果是 14,203,266。 What's wrong with the code above?上面的代码有什么问题? Thanks a lot for any hint.非常感谢任何提示。

EDIT: below table strusture...same for both tables编辑:下表结构...两个表相同在此处输入图像描述

First, I'd drop Code Igniter's DB methods.首先,我会放弃 Code Igniter 的 DB 方法。 They're (in my view) obstructive and useless for anything except basic query structures.它们(在我看来)对于除了基本查询结构之外的任何东西都是阻碍和无用的。

With that in mind, you can achieve what you want with something like this:考虑到这一点,您可以通过以下方式实现您想要的:

$sql = '
SELECT
    (SELECT COUNT(id) FROM `'.$this->table_images_civil.'`) AS tbl1Count,
    (SELECT COUNT(id) FROM `'.$this->table_images_military.'`) AS tbl2Count';
$request = $this->regs_db->query($sql);
$arr = $request->fetch_assoc();
$this->db->from('yourtable');
[... more active record code ...]
$query = $this->db->get();
$rowcount = $query->num_rows();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM