开发者

Grouping Records with the same value

I am trying to create a conversations based messaging system.

I want to group all messages that h开发者_如何学运维ave the same conversation_id so that when I display a list of current conversations you only see the latest message from each conversation.

Can I group the values in the mysql query, or would I have to do it in the php?


select m.convos_id, m.message_content from messages m  
          where m.id in 
          (select MAX(m1.id) from messages m1 GROUP BY m1.convos_id)


Thanks for your help, this is what I have ended with.

SELECT messages.*, messages_conversations.subject
FROM messages, messages_conversations
WHERE messages.to_user_id = '".$userid."'
AND messages_conversations.id = messages.conversation_id
GROUP BY messages.conversation_id


You can group that in mysql query something like t this:

select * from table where conversation_id = id_here

This will get all the records that have conversation_id set to id_here and then you can use php to work on that array/group.


Its a basic example of parent child relation in database. each message much have unique id and iys should also refer to the parent key of convo id.

then you can get the latest message for a given convo id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜