i have a chat sql table with the structure as following :
DROP TABLE IF EXISTS `chat`;
CREATE TABLE IF NOT EXISTS `chat` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`message` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`idsender` int(11) DEFAULT NULL,
`sentTime` datetime NOT NULL,
`idreceiver` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
)
how to get the latest message sent to the user with the id = 2 from every user he has talked with (where the idreceiver = 2) ?
this is my try , it's getting me a message from every user the user with the id = 2 has talked with but it's not the last message :
SELECT * FROM chat
WHERE idreceiver = 2
GROUP BY idsender
ORDER BY sentTime DESC