I have a message table with fields id, time, isread, message, messagestatus, receiver and user. I want to retrieve the latest message (based on time) a user with an id of 1 sent or received to any other users.
for example if a user with id 1(sender) sends a message to user with id 2(receiver) and if user with id 2 sends user with id 1 a message. only the most current message sent between both of them should be picked
My already existing table looks like this :
i tried SELECT * from (SELECT MAX(time) as time ,sender,receiver from message group by sender,receiver) as f where receiver=1 or sender=1 limit 20
but it only displays the sender, receiver and time while other columns like message, id, messagestatus and read are omitted, plus it does not find the general last message sent between two users, but instead finds the most recent message sent between user1 and another user when user1 is the sender and the other user is the receiver and also finds the last message when user1 is the receiver and the other user is the sender.

ORDER BY time DESC LIMIT 1instead of using aggregation. If you want this for multiple users in a single query, use windowed / analytic functions, and update your question to clarify that.