2016-01-02 64 views
0

我正在使用CodeIgniter的私人消息應用程序。
我有3個表:將SQL查詢轉換爲codeigniter活動記錄

  1. 對話
  2. conversations_messages
  3. conversations_members。

我試圖通過匹配對話ids聯接表,並獲取所有誰在登錄的用戶的消息。

這是標準的SQL查詢

SELECT 
    'conversations','conversation_id', 
    'conversations','conversation_subject', 
    MAX('conversations_messages','message_date') AS 'conversation_last_reply' 
FROM 'conversations' 
LEFT JOIN 'conversations_messages' ON 'conversations'.'conversation_id' = 'conversations_messages'.'conversation_id' 
INNER JOIN 'conversations_members' ON 'conversations'.'conversation_id' = 'conversations_members'.'conversation_id' 
WHERE 'conversations_members', 'user_id' = $sender_id 
AND 'conversations_members','conversation_deleted' = 0 
GROUP BY 'conversations'.'conversation_id' 
ORDER BY 'conversation_last_reply' DESC"; 

它的一大查詢,因此;我不知道如何將其轉換爲CodeIgniter活動記錄,或者只是簡單地將它與框架一起運行。

任何更正將不勝感激。

+1

嘗試查詢在phpMyAdmin,看看發生了什麼錯誤。有一件事我注意到了。你在哪裏使用''conversations_members','user_id'= $ sender_id「而不是'conversations_members.user_id'= $ sender_id。 –

+0

謝謝....我用它,它的工作就像一個魅力 –

+1

如果你覺得它有用,那麼請給予upvote。 –

回答

3

請嘗試以下代碼與CodeIgniter有效記錄。

$this->db->select('conversations, conversation_id, conversation_subject'); 
$this->db->select_max('conversations_messages.message_date', 'conversation_last_reply'); 
$this->db->from('conversations'); 
// Joining with conversations_messages table 
$this->db->join('conversations_messages','conversations.id=conversations_messages.conversation_id','left'); 
$this->db->join('conversations_members','conversations.id= conversations_members.conversation_id','inner'); 
$this->db->where('conversations_members.user_id',$sender_id); 
$this->db->where('conversations_members.conversation_deleted','0'); 
$this->db->group_by('conversations.conversation_id'); 
$this->db->order_by('conversation_last_reply'); 
// Get the results from db 
$query = $this->db->get(); 
// Result array contains the records selected 
$result_array = $query->result_array(); 

快樂編碼

納西姆

+0

看起來像這樣會工作,但還有一個錯誤。 「列表中的字段對話未知字段」。我確信我的數據庫中有對話。順便說一下,對話,conversations_members和conversations_messages是我數據庫中的表格。感謝您的幫助naseem! –

+0

你能否檢查轉換表是否有字段「對話」..?如果現在一切都安然無恙,不要忘記標記我的答案爲正確的答案,併爲我的答案投票。 –

+0

Rich,我更新了代碼。在選擇查詢中有兩個對話。我剛剛刪除它。 –

0
$this->db->select('conversations, conversation_id, conversation_subject'); 
$this->db->select_max('conversations_messages.message_date', 'conversation_last_reply'); 
$this->db->from('conversations'); 
// Joining with conversations_messages table 
$this->db->join('conversations_messages','conversations.id=conversations_messages.conversation_id','left'); 
$this->db->join('conversations_members','conversations.id= conversations_members.conversation_id','inner'); 
$this->db->where('conversations_members.user_id',$sender_id); 
$this->db->where('conversations_members.conversation_deleted','0'); 
$this->db->group_by('conversations.conversation_id'); 
$this->db->order_by('conversation_last_reply'); 
// Get the results from db 
$query = $this->db->get(); 
// Result array contains the records selected 
$result_array = $query->result_array(); 
相關問題