I have created a messaging system for users, it allows them to send a message to another user. If it is the first time they have spoken then a new conversation is initiated,
I haven't tested this approach as I don't have access to mysqldb right now. But, I think you should be able to get this done by using a ranking function. Since mysql doesn't have an equivalent of row_number function of Oracle I think you can do it like this:
Select * from (
Select
uc.id,
uc.user_id,
uc.friend_id
um.message
um.read,
um.time,
@rownum := IF(@prev_val = um.conversation_id, @rownum + 1, 1) AS rank,
@prev_val := um.conversation_id
From
userconversation uc,
usermessages um,
(select @row_num:=1) rows,
(select @prev_val:='') partitions
Where
uc.id=um.conversation_id
and c.userId = 222 OR c.friendId = 222
Order By
um.conversation_id,um.id desc
)t where t.rank=1