0

i am having troubles translating this database 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";

to codeignitor's active records .

I have tried it this way

$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');
   ........

But i get stack at left and inner join . So i tried it this way

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

but there's errors everywhere.

2 Answers 2

1

You have mistaken in quote

$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();
Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this

// 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.