【问题标题】:Converting SQL query to codeigniter active record将 SQL 查询转换为 codeigniter 活动记录
【发布时间】:2016-01-02 18:43:18
【问题描述】:

我正在使用 CodeIgniter 开发一个私人消息应用程序。
我有 3 张桌子:

  1. 对话
  2. conversations_messages
  3. conversations_members。

我正在尝试通过匹配对话ids 来加入表格,并为已登录的用户获取所有消息。

这是标准的 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 活动记录,或者只是让它与框架一起运行。

任何更正也将不胜感激。

【问题讨论】:

  • 在 phpmyadmin 中尝试查询,看看发生了什么错误。我注意到一件事..您在 where 条件下使用了“'conversations_members', 'user_id' = $sender_id” 而不是 'conversations_members.user_id' = $sender_id。
  • 谢谢....我用过它,效果很好
  • 如果觉得有用,请点赞。

标签: php mysql codeigniter


【解决方案1】:

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

快乐编码

纳西姆

【讨论】:

  • 似乎这样可以工作,但还有一个错误。 “列表中的字段对话未知字段”。我确信我在我的数据库中有对话作为表。顺便说一下,conversations、conversations_members 和 conversations_messages 是我数据库中的表。谢谢你的帮助!
  • 您能否检查一下转换表是否有字段''conversations' ..?如果现在一切正常,不要忘记将我的答案标记为正确答案并为我的答案投票。
  • Rich,我已经更新了代码。选择查询中有两个对话。我刚刚删除了它。
【解决方案2】:
 $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();

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多