Php 将SQL查询转换为codeigniter活动记录
我正在使用CodeIgniter开发一个私人消息应用程序。Php 将SQL查询转换为codeigniter活动记录,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在使用CodeIgniter开发一个私人消息应用程序。 我有三张桌子: 对话 对话与信息 与联合国成员的对话 我试图通过匹配对话id来加入表,并为登录的用户获取所有消息 这是标准的sql查询 SELECT 'conversations','conversation_id', 'conversations','conversation_subject', MAX('conversations_messages','message_date') AS 'conv
我有三张桌子:
id
来加入表,并为登录的用户获取所有消息
这是标准的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活动记录,或者仅仅使其与框架一起运行
任何更正都将不胜感激。使用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();
快乐编码
Naseem看起来这会起作用,但还有一个错误。“字段对话列表中的未知字段”。我确信我的数据库中有作为表的对话。顺便说一下,对话、对话成员和对话消息都是我数据库中的表。谢谢你的帮助naseem!请检查转换表中是否有“对话”字段。。?如果现在一切正常,别忘了将我的答案标记为正确答案并投票支持我的答案。Rich,我已经更新了代码。select查询中有两个对话。我刚刚删除了它。请在phpmyadmin中尝试您的查询,看看发生了什么错误。我注意到一件事。。您使用了“'conversations\u members'、'user\u id'=$sender\u id”而不是“conversations\u members”。在where条件下,user\u id'=$sender\u id。谢谢。。。。我用了它,它就像一个charmif你觉得它有用,然后请放弃投票。
$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();