6

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

数据库

user_table
- cwid
- first_name
- last_name
- user_image_link

message
- id
- cwid1 (sender id)
- cwid2 (receiver id)
- message (消息内容)
- messagetime (timestamp)
- 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

4 回答 4

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

如果有人可以,请改进。

于 2013-10-20T10:33:34.603 回答
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 在这种情况下不会引发错误,但非聚合列中的值是随机的。

于 2012-10-25T20:51:10.313 回答
0

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

于 2012-12-11T01:05:21.267 回答
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 
于 2016-09-30T14:05:36.173 回答