2016-01-02 37 views
0

我有麻煩翻譯這個數據庫查詢左和內CodeIgniter的數據庫查詢加入

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"; 

到codeignitor的活動記錄。

我已經嘗試過這種方式

$this->db->select('conversation_id, conversation_subject'); 

    $this->db->get('conversations'); 

    $this->db->select_max('message_date', 'conversation_last_reply'); 

    $this->db->get('conversations_messsages'); 
    $this->db->from('conversations'); 
    ........ 

但我在左,內得到堆棧加入。所以我這樣試了

$query = $this->db->query(
    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" 
     ); 
    return $query->result(); 

但是到處都是錯誤。

回答

1

你在報價誤

$query = $this->db->query("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" 
     ); 
    return $query->result();