php - group messages by same users -
i have 2 tables .. 1- users,,2-messages
i wrote query doesn't show last subject , need last subject
select ( case when messages.sender = 68314 messages.receiver else messages.sender end ) user_id, max(messages.added) last_added,messages.subject, max(messages.id) last_id,users.username messages inner join users on users.id = if(messages.sender = 68314, messages.receiver, messages.sender) (messages.sender = 68314 or messages.receiver = 68314) , messages.sender!=0 group ( case when messages.sender = 68314 messages.receiver else messages.sender end ) order last_added desc
it shows first subject, not last one.
please give query try. first select max(id) of messages of people talking id 68314 assuming created last...and joins messages, , joined users other person's name.
select last_messages.user_id, m.added last_added, m.subject, last_messages.last_id, u.username messages m inner join (select if(sender = 68314,receiver,sender) user_id, max(id) last_id messages if(sender = 68314,sender,receiver) = 68314 group user_id )last_messages on last_messages.last_id = m.id inner join users u on last_messages.user_id = u.id order last_added;
Comments
Post a Comment