Mysql Codeigniter活动记录计数返回null
我需要从一个表中选择一些数据,并计算另一个表中的相关图像。 我使用的代码是模型文件中的以下代码Mysql Codeigniter活动记录计数返回null,mysql,codeigniter,count,Mysql,Codeigniter,Count,我需要从一个表中选择一些数据,并计算另一个表中的相关图像。 我使用的代码是模型文件中的以下代码 $this->rci_db->select (" $this->tbl_register.id, $this->tbl_register.cor, DATE_FORMAT($this->tbl_register.registerdate, '%d/%m/%Y') AS registerdate, $this
$this->rci_db->select ("
$this->tbl_register.id,
$this->tbl_register.cor,
DATE_FORMAT($this->tbl_register.registerdate, '%d/%m/%Y') AS registerdate,
$this->tbl_registrations.registration,
$this->tbl_aircrafts.cn,
$this->tbl_aircrafts.built,
$this->tbl_manufacturers.manufacturer,
$this->tbl_models.type AS model,
COUNT($this->tbl_images.imgid) AS count
");
$this->rci_db->from("$this->tbl_register");
$this->rci_db->join("$this->tbl_registrations", "$this->tbl_registrations.rid = $this->tbl_register.rid", 'left');
$this->rci_db->join("$this->tbl_aircrafts", "$this->tbl_register.aid = $this->tbl_aircrafts.aid", 'left');
$this->rci_db->join("$this->tbl_manufacturers", "$this->tbl_manufacturers.mid = $this->tbl_aircrafts.mid", 'left');
$this->rci_db->join("$this->tbl_models", "$this->tbl_models.tid = $this->tbl_aircrafts.tid", 'left');
$this->rci_db->join("$this->tbl_images", "$this->tbl_register.id = $this->tbl_images.id", 'left');
$this->rci_db->where("$this->tbl_register.rid", $rid);
$query = $this->rci_db->get();
if ($query->num_rows() > 0)
{
return $query->result();
}
return false;
查询为COUNT语句返回null,我无法找出错误所在。
其他数据返回正确
感谢您的帮助。该计数在另一个表中,因此您必须在查询中以某种方式关联这些计数。在这种情况下,只需使用子选择,而不是加入 在您的选择中类似于以下内容:
$this->rci_db->select ("
$this->tbl_register.id,
$this->tbl_register.cor,
DATE_FORMAT($this->tbl_register.registerdate, '%d/%m/%Y') AS registerdate,
$this->tbl_registrations.registration,
$this->tbl_aircrafts.cn,
$this->tbl_aircrafts.built,
$this->tbl_manufacturers.manufacturer,
$this->tbl_models.type AS model,
(SELECT COUNT($this->tbl_images.imgid) FROM $this->tbl_images WHERE $this->tbl_register.id = $this->tbl_images.id) AS count
");