[英]Codeigniter 3 - Count rows from one table and join
I am trying to join two table and count all results from messageFiles which are related to message I have tables with following structure: 我试图加入两个表并计算来自messageFiles的所有结果,这些结果与message有关,我有具有以下结构的表:
messages : 讯息 :
id
name
email
subject
message
date_added
read
messagesFiles messagesFiles
id
name
message_id
date_added
I am trying this code, but I always get result countFiles = 1 and I message is repeated for every file that is related to it (for example if I have 3 files for the message, it will be repeated 3 times). 我正在尝试此代码,但是我总是得到结果countFiles = 1,并且对于与之相关的每个文件都会重复我的消息 (例如,如果我有3个文件用于消息,它将重复3次)。 Also messages which doesn't have files will not be selected by this query. 同样,没有文件的消息也不会被该查询选择。 What's seems to be a problem? 似乎有什么问题?
$this->db->select("SQL_CALC_FOUND_ROWS *, messages.*, COUNT(messagesFiles.id) as countFiles", FALSE)->from('messages')
->join('messagesFiles', "messagesFiles.message_id = messages.id")
->where("messages.read", 1)
->group_by('messagesFiles.id')->get()->result_array();
You could try some thing like this code below and add return $this->db->count_all_results()
. 您可以尝试下面的代码,并添加return $this->db->count_all_results()
。
http://www.codeigniter.com/docs http://www.codeigniter.com/docs
http://www.codeigniter.com/userguide2/database/active_record.html http://www.codeigniter.com/userguide2/database/active_record.html
public function count_test() {
$this->db->select("*");
$this->db->from('category c');
$this->db->join('category_description cd', "cd.category_id = c.category_id");
return $this->db->count_all_results();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.