1

我正在尝试加入两个表并计算与消息相关的messageFiles中的所有结果 我有以下结构的表:

留言

id
name
email
subject
message
date_added
read

消息文件

id
name
message_id
date_added

我正在尝试这段代码,但我总是得到结果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();
4

1 回答 1

0

您可以尝试下面的代码并添加return $this->db->count_all_results().

http://www.codeigniter.com/docs

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();
}
于 2015-05-01T03:04:10.433 回答