MYSQL - FInd last date and group by IDUSER

132 views Asked by At

I have a message system on my site and I would like to see a list of users who sent me a message while displaying only the last message of each user. Same way as facebook.

So, I have a table with the IDUSERSENDER, IDUSERRECEIVER, DATE AND MESSAGE

I tried on several time to take a query but I can not have the last message of each.

Idea?

2

There are 2 answers

0
Isaac On BEST ANSWER

ok, again, try this please

SELECT MESSAGE FROM MY_TABLE WHERE IDUSERRECEIVER = 'MY_ID' AND DATE IN (SELECT MAX(DATE) FROM MY_TABLE GROUP BY IDUSERSENDER); 
0
Richard On

This easy query should work:

SELECT * FROM messages WHERE receiver = 2 GROUP BY sender ORDER BY date DESC LIMIT 1