簡體   English   中英

Codeigniter 3-從一個表計數行並聯接

[英]Codeigniter 3 - Count rows from one table and join

我試圖加入兩個表並計算來自messageFiles的所有結果,這些結果與message有關,我有具有以下結構的表:

訊息

id
name
email
subject
message
date_added
read

messagesFiles

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

您可以嘗試下面的代碼,並添加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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM