Answer the question
In order to leave comments, you need to log in
There is a chat. How to display a list of conversations with the last received or sent message?
Tell me, there is a chat. How to display a list of conversations with the last received or sent message? The base structure is as follows:
The principle is as follows: the bot sends a message to the user and vice versa.
I tried this, but it's not the right solution:
$viewLastMsg = R::findAll('chats', 'ORDER BY id DESC LIMIT 1');
foreach ($viewLastMsg as $vLM)
{
if(
($vLM->sender_type == 'user' && $vLM->sender_id == $_SESSION['logged_user']->id && $vLM->recipient_id == $bot->id)
||
($vLM->sender_type == 'bot' && $vLM->sender_id == $bot->id && $vLM->recipient_id == $_SESSION['logged_user']->id)
)
{
echo $vLM->message;
}
}
Answer the question
In order to leave comments, you need to log in
Since you didn't specify a database, here's a solution for you, but not all window functions are supported =)
select distinct
first_value(message) over (partition by LEAST(sender_id, recipient_id),GREATEST(sender_id, recipient_id)
order by id desc)
from table
select message
from table
where id in (select max(id) from table group by LEAST(sender_id, recipient_id),GREATEST(sender_id, recipient_id))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question