I want the solution like each item id all messages between sender and receiver and need all rows belongs to user i.e. if sender id = 2 then it will list out all message of this user id whether user in sender or receiver that belongs to item id.
`
I tried this query but its returning other id messages as well but i want all rows where user id = 2 messages.
SELECT c1.id, c1.sender, c1.receiver, c1.message, c1.item_id, c1.created_at FROM chats c1 INNER JOIN ( SELECT item_id, MAX(id) AS max_id FROM chats WHERE sender = 2 OR receiver = 2 GROUP BY item_id ) c2 ON c1.id = c2.max_id ORDER BY c1.created_at DESC;