Mohatir S
Mohatir S

Reputation: 5

SELECT GROUP BY latest entry in INBOX msg sent by user

i am building a simple PM for my personal website where a user can join and send message to the registered users on my website,

i am stuck in grouping and selecting latest user (by latest msg time) in INBOX page

my table :

id   msg_from   msg_to  msg   date   in_del   out_del

i want to show latest user's id each in a group

here is my SQL query :

SELECT ttalk.id, ttalk.msg_from,  users.first_name, users.last_name
FROM ttalk
INNER JOIN users ON ttalk.msg_from = users.id 
WHERE ttalk.msg_to = '$_SESSION[user_id]' AND ttalk.in_del='0'
GROUP BY ttalk.msg_from DESC LIMIT 500

FOR EXAMPLE : in facebook when a old friend send msg then his/her thread goes on top

Thanks in advance :-)

Upvotes: 0

Views: 90

Answers (1)

GautamD31
GautamD31

Reputation: 28763

It will be ORDER BY with DATE like

GROUP BY ttalk.msg_from ORDER BY date DESC LIMIT 500

Secondary You can also ORDER BY with id if it is an auto increment but not much suggestable when you are having the DATE field.

Upvotes: 1

Related Questions