Php Codeigniter 3-从一个表中计数行并连接

Php Codeigniter 3-从一个表中计数行并连接,php,mysql,codeigniter,codeigniter-3,Php,Mysql,Codeigniter,Codeigniter 3,我正在尝试联接两个表,并统计来自与消息相关的消息文件的所有结果 我有以下结构的表格: 信息: 消息文件 我正在尝试这段代码,但我总是得到结果countFiles=1,并且我的消息会对与它相关的每个文件重复。例如,如果我有3个文件用于消息,它将重复3次。此外,此查询不会选择没有文件的邮件。有什么问题吗 $this->db->select("SQL_CALC_FOUND_ROWS *, messages.*, COUNT(messagesFiles.id) as countFiles",

我正在尝试联接两个表,并统计来自与消息相关的消息文件的所有结果 我有以下结构的表格:

信息:

消息文件

我正在尝试这段代码,但我总是得到结果countFiles=1,并且我的消息会对与它相关的每个文件重复。例如,如果我有3个文件用于消息,它将重复3次。此外,此查询不会选择没有文件的邮件。有什么问题吗

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

您可以尝试下面的代码,然后添加return$this->db->count\u all\u结果


你要回来几排?如果删除COUNT和group_by,您将获得多少行?总共有8行没有COUNT和group_by,您如何使用COUNT和group_by返回?14-这是messageFiles表中的行数。这没有意义。。。使用group_by时,行数应少于不使用group_by时的行数
id
name
message_id
date_added
$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();
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();
}