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

Count all rows from two MySQL tables in Codeigniters

您好需要计算两个 MySQL table 中的所有行。 表格具有相同的结构,我在模型文件中使用的代码如下:

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

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

我得到的结果根本不正确。我知道第一个 table 有 10,934 行,第二个有 1,299 行......但我得到的结果是 14,203,266。 上面的代码有什么问题? 非常感谢任何提示。

编辑:在 table 结构下...两个 tables 相同

首先,我将放弃 Code Igniter 的数据库方法。它们(在我看来)对除基本查询结构之外的任何东西都是阻碍和无用的。

考虑到这一点,您可以通过以下方式实现您想要的:

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