数据库查询以从每个用户获取最后一条消息(如facebook消息)

时间:2012-10-25 20:00:38

标签: php mysql message

我需要在登录用户和他曾与之聊过的任何用户之间获取最后一条消息,就像facebook一样。这将像facebook.com/messages页面。 这是我到目前为止所做的事情

数据库

USER_TABLE
- cwid
- first_name
- last_name
- user_image_link

消息
- id
- cwid1(发件人ID)
- cwid2(接收者ID)
- 消息(消息内容)
- messagetime(时间戳)
- userread(enum'Y''N')`

查询 登录用户的ID = 1

SELECT    
user_table.cwid,    
    message.cwid1,   
    message.cwid2,   
    message.message,   
    message.userread,  
    MAX(message.messagetime),   
    user_table.first_name,   
    user_table.last_name,   
    user_table.user_image_link   
    FROM message   
    INNER JOIN user_table   
    ON message.cwid1 = user_table.cwid   
    WHERE (cwid2="1")   
    GROUP BY cwid1   
    ORDER BY messagetime DESC   

这将返回用户从向他发送消息的所有人收到的最后消息。我还需要同样获取此用户发送的所有消息,可以通过

完成
SELECT    
user_table.cwid,   
    message.cwid1,     
    message.cwid2,    
    message.message,   
    message.userread,    
    MAX(message.messagetime),   
    user_table.first_name,    
    user_table.last_name,    
    user_table.user_image_link    
    FROM message    
    INNER JOIN user_table    
    ON message.cwid1 = user_table.cwid   
    WHERE (cwid1="1")    
    GROUP BY cwid2    
    ORDER BY messagetime DESC    

我需要他们两个混合不同的用户,并按消息时间排序就像Facebook消息。我是MySql的新手,任何帮助非常感谢。谢谢!

4 个答案:

答案 0 :(得分:3)

我更多地研究了它并进行了这个查询。 - 它列出了来自最近用户的消息,他们向我发送了消息或收到了我的消息 - 它还包括来自群聊的消息,群组可以由任何人创建,群组中的任何人都可以邀请他们的朋友 - 它还包括读/未读标志,以突出显示前端未读消息。

这不是最有效或最优雅的查询,但它有效。任何改进都非常受欢迎

SELECT * 
FROM (
 SELECT * 
FROM (

SELECT * 
FROM (

SELECT users.id,'0' AS groupId, users.name,  users.profilePic,  messages.time,messages.message , 'Y' AS unread 
FROM users
INNER JOIN messages ON messages.userId2 = users.id
WHERE messages.userId1 = '" . $userId . "'
UNION 
SELECT users.id,'0' AS groupId, users.name,users.profilePic, messages.time, messages.message , messages.read AS unread
FROM users
INNER JOIN messages ON messages.userId1 = users.id
WHERE messages.userId2 = '" . $userId . "'
) AS allUsers
ORDER BY TIME DESC
) AS allUsersSorted
GROUP BY id
UNION
select * from(
SELECT '0' AS id, msgGroups.id AS groupId, msgGroups.name, msgGroups.image AS profilePic, IF(userGroupMsg.time IS NULL,userGroups.createdOn,userGroupMsg.time )   AS time,IF(userGroupMsg.message IS NULL,'',userGroupMsg.message ), 'Y' AS unread
FROM msgGroups
LEFT JOIN userGroups ON msgGroups.id = userGroups.groupId
LEFT JOIN userGroupMsg ON msgGroups.id = userGroupMsg.groupId
WHERE userGroups.userId = '" . $userId . "'
ORDER BY time DESC

)AS allUsersSorted
GROUP BY groupId

 )AS allSorted
ORDER BY TIME DESC

如果有人可以,请改进。

答案 1 :(得分:1)

也许是这样的:

SELECT
  DISTINCT m.id, m.cwid1, m.cwid2, m.message, m.userread, m.messagetime,
  u.first_name, u.last_name, u.user_image_link
FROM
  user_table u,
  message m,
  (
    SELECT DISTINCT IF(cwid1 = "1", cwid2, cwid1) AS id
    FROM message
    WHERE "1" IN (cwid1, cwid2)
  ) partner
WHERE
  u.cwid = partner.id
  AND (m.messagetime, m.cwid1, m.cwid2) IN
    (
      SELECT MAX(messagetime), cwid1, cwid2
      FROM message
      WHERE (cwid1, cwid2) IN ((partner.id,"1"),("1",partner.id))
      GROUP BY cwid1, cwid2
    )
ORDER BY m.messagetime DESC

请注意,FROM子句中未出现在MAX 等聚合函数中的所有列都必须出现在GROUP BY子句中。在这种情况下,MySQL不会引发错误,但非聚合列中的值是随机的。

答案 2 :(得分:0)

我会看看Facebook如何在这里为消息设置FQL表:https://developers.facebook.com/docs/reference/fql/unified_message。并以此为基础构建您的桌面。

答案 3 :(得分:0)

Try this
select m.* ,u.*
            from
                messages m
                inner join (
                select max(id) as maxid
                from messages
                where messages.list_id = " '. $data['list_id'] .'" 
                group By (if(sender_id > reciever_id,  sender_id, reciever_id)), 
                (if(sender_id > reciever_id,  reciever_id, sender_id))
               ) t1 on m.id=t1.maxid