简体   繁体   English

将SQL查询转换为codeigniter活动记录

[英]Converting SQL query to codeigniter active record

I am working on a private message app using CodeIgniter. 我正在使用CodeIgniter开发私人消息应用程序。
I have 3 tables: 我有3张桌子:

  1. conversations 对话
  2. conversations_messages 对话消息
  3. conversations_members. 对话成员。

I am trying to join the tables by matching the conversations ids and fetch all the messages for the user who's logged in. 我正在尝试通过匹配对话ids来加入表,并为登录用户获取所有消息。

This is the standard sql query 这是标准的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";

Its a big query, hence; 因此,这是一个很大的查询; I'm not sure how to convert it to CodeIgniter active records or simply make it run with the framework. 我不确定如何将其转换为CodeIgniter活动记录或仅使其与框架一起运行。

Any corrections would be appreciated too. 任何更正也将不胜感激。

Try below code with CodeIgniter active record. 尝试使用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();

Happy coding 快乐编码

Naseem 纳西姆

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM