global_ref_id int(12)
to int(12)
from int(12)
message text
status int(1)
viewed int(1)
where global_ref_id is id of messages (foreign key from table events), to is user id of user who received message, from is user id of user who send the message.
I want to generate a query which gives me result displaying latest message from each user to a user (say whose user_id is 192) also giving total number of messages from each user to user 192.
Till now I came up with this query to get results:
SELECT messages.*,events.event_time, COUNT(messages.from) as "Total number of Messages to User" FROM messages, events WHERE events.global_id = messages.global_ref_id AND messages.to = 192 GROUP BY messages.from ORDER BY events.event_time DESC
now problem is that, this query is displaying the oldest message instead of latest message what I need is Latest message.
Using MYSql version:5.5.25a
For easy understanding I have created table in sql fiddle:
http://sqlfiddle.com/#!2/c68dc/7











