У меня проблемы с переводом этого запроса к базе данных

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

Но ошибки есть везде.

0
Rich fiifi 2 Янв 2016 в 09:13

2 ответа

Лучший ответ

Вы ошиблись в цитате

$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
Ajay Makwana 2 Янв 2016 в 06:18

Вы можете попробовать что-то вроде этого

// left, right, outer, inner, left outer, and right outer
$this->db->join('comments', 'comments.id = blogs.id', 'left');

https://www.codeigniter.com/userguide3/database/query_builder.html

1
Mourad MAMASSI 8 Ноя 2017 в 12:22