简体   繁体   English

我如何在 codeigniter sql 中实现以下条件?

[英]how do i achieve following condition in codeigniter sql?

I have a table with the name message_system.我有一个名为 message_system 的表。

and structure of the table is as follow:表的结构如下:

    message_system => message_id(primary key)->sender_id->receiver_id->
               message->time(current time stamp)

lets consider a $user_id = 1 and I want to select the row from the table something like让我们考虑一个$user_id = 1并且我想从表中选择类似的行

select * from message_system where sender_id = $user_id or receiver_id = $user_id  

and from the yielded result I want select only single result which has recently stored according to time stamp.从产生的结果中,我只想选择最近根据时间戳存储的单个结果。

actually I want to select recent message from the message thread between two user..实际上我想从两个用户之间的消息线程中选择最近的消息..

edit 1:编辑1:

I want to get recent message from the table with respect to each unique pair of (user1_id and user2_id).我想从表中获取关于每个唯一对(user1_id 和 user2_id)的最新消息。 Either it is(user1_id, user2_id) or (user2_id, user1_id) usually user id of logged in user would be on of the id and another is id of user who have been messaged to current logged in user.无论是 (user1_id, user2_id) 还是 (user2_id, user1_id) 通常登录用户的用户 id 将在 id 上,另一个是已向当前登录用户发送消息的用户的 id。

edit 2:编辑2:

example:例子:

lets consider user1 is logged in and he has messaged to user2.让我们考虑用户 1 已登录并且他已向用户 2 发送消息。 and later messaged to user3 and user4.然后向 user3 和 user4 发送消息。 and those all messages are stored in the table and now i want to query such that it retrieves only recent message with user2,user3,user4 in user1 wall.并且那些所有消息都存储在表中,现在我想进行查询,以便它仅检索 user1 墙中包含 user2、user3、user4 的最近消息。 not all their history不是他们的全部历史

edit 3 :编辑3:

query should return recent message of logged in user with user1 and recent message of logged in user user2 and user3 so on.查询应返回登录用户 user1 的最近消息和登录用户 user2 和 user3 的最近消息,依此类推。

thank you for your time.感谢您的时间。

select * from message_system where sender_id = $user_id or receiver_id = $user_id

would translate in CodeIgniter in:将在 CodeIgniter 中翻译为:

$this->db->where('sender_id', $user_id);
$this->db->or_where('receiver_id', $user_id);
$query = $this->db->get('message_system');
$row = $query->row();

If you want the most recent input, you could order by time , and if you know you only need one row, limit to 1:如果你想要最近的输入,你可以按time排序,如果你知道你只需要一行,限制为 1:

$this->db->where('sender_id', $user_id);
$this->db->or_where('receiver_id', $user_id);
$this->db->order_by('time', 'desc');//order by time
$this->db->limit('1');//limit the query to only one row
$query = $this->db->get('message_system');
$row = $query->row();// get only first

UPDATE: The solution above works if you want to find one entry corresponding to one given user.更新:如果您想找到与给定用户对应的条目,则上述解决方案有效。 If you want to find last entry for a given pair of users, thing would look like this if unknown who would be the sender and who would be te receiver:如果您想查找给定用户对的最后一个条目,如果不知道谁是发送者和谁是接收者,事情将如下所示:

$this->db->where_in('sender_id', array($user_id1, $user_id2));
$this->db->where_in('receiver_id', array($user_id1, $user_id2));
$this->db->order_by('time', 'desc');//order by time
$this->db->limit('1');//limit the query to only one row
$query = $this->db->get('message_system');
$row = $query->row();// get only first

And like this if you know exactly who is who:如果你确切地知道谁是谁,就像这样:

$this->db->where('sender_id', $user_id1);
$this->db->where('receiver_id', $user_id2);
$this->db->order_by('time', 'desc');//order by time
$this->db->limit('1');//limit the query to only one row
$query = $this->db->get('message_system');
$row = $query->row();// get only first

With little bit modification of the table I have come up with an idea.通过对表格的一点点修改,我想出了一个主意。

earlier my table structure was:早些时候我的表结构是:

message_system => message_id(primary key)->sender_id->receiver_id->
               message->time(current time stamp)

after modification:修改后:

message_system => message_id(primary key)->sender_id->receiver_id->
               message->time(current time stamp)->unique_pair_key

unique_pair_key is to identify the message between two user uniquely and value would be something like this ($sender_id^2 + $receiver_id^2) which is unique for given pair. unique_pair_key是唯一标识两个用户之间的消息,值将是这样的($sender_id^2 + $receiver_id^2) ,对于给定的对是唯一的。

all messages between two particular user would be identify by this unique_pair_key.两个特定用户之间的所有消息都将由这个 unique_pair_key 标识。

so taking logged_in user as the reference, with respect to him only recent message with different user (if there any) should be shown (similar to stack overflow recent inbox message )and on clicking that particular message it should direct to their message thread.因此,以登录用户为参考,对于他而言,只应显示与不同用户(如果有的话)的最近消息(类似于堆栈溢出最近收件箱消息),并且在单击该特定消息时,它应该指向他们的消息线程。

model/model.php模型/model.php

     public function get_notification($user_id)
          {

       $this->db->where("receiver_id",$user_id);
       $this->db->where("sender_id",$user_id); 

    //because recent message would either be sent or received by logged in user

    $this->db->select_max('message_id');

   // latest message would have greatest value of id

    $this->db->group_by('unique_pair_key');
    $query = $this->db->get('message_system ');

   //returns latest message_id with unique pair in which on of  them is $userid.
}

above query only generates message id with that we can further query to get message上面的查询只生成消息id,我们可以进一步查询以获取消息

If you have any better solution please share with us.如果您有更好的解决方案,请与我们分享。

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

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