There is a messageprivate table with the structure:
id | from_user | to_user | message | date | status | it stores messages. From the structure it is clear that from_user is the user who wrote the message, to_user - to whom he wrote it, message - the message itself, date - when it wrote and status - whether it was read or not, by default this field is 0, and when the user is was written it reads it changes to 1.
It is necessary to write a sql request that will output messages of the grouped type as in a contact when we go into the message section - i.e. we see the list of correspondence with each user, namely the last message from each user and the number of unread messages.
Initially, we know our id - i.e. when we write to someone, our id gets into the from_user field, and when someone writes us, then our id gets into the to_user field.
3 hours already down the drain ...
Thank!
select count(1) as count_unreaded from messageprivate where status = 0 and to_user = OUR_USER_IDENTIFICATOR group by from_user? it would be better to put the test data on sqlfiddle.com - Alexey Shimansky