我已经使用PHP和mysql创建了一个小型消息传递系统。我已在消息表中将我的用户与sender
和receiver
相关联。我可以列出2个用户之间的对话。但是我无法通过每种对话显示1条消息结果的方法!
假设我有用户1,2和3.所以我的消息表可能如下所示:
===================================================================
ID || sender || receiver || text || date
===================================================================
1 || 1 || 2 || Hello! || timestamp stuff
2 || 2 || 1 || Hi! ||
3 || 1 || 3 || Boo! ||
所以对话链接为
SELECT * FROM messages WHERE (sender=1 OR sender=2) AND (receiver=1 OR receiver=2);
这样我就能得到2个用户之间的完整对话(非常简化)。
回到问题但是,如何从每个对话中获得1条消息?
我尝试过这样的事情:
(SELECT m.text,m.date,sender.username,receiver.username
FROM messages AS m
INNER JOIN users AS sender on m.sender=user.ID
INNER JOIN users AS receiver ON receiver.ID=m.receiver
WHERE m.sender=1 OR m.receiver=1
ORDER BY messages.date DESC LIMIT 1);
但显然这并不是我想要的,因为只返回一行。我想我知道我需要做什么,我只是不知道该怎么做? 我想我需要这样的东西:
SELECT sender.username,receiver.username,m.text,m.date FROM messages AS m
INNER JOIN users AS sender ON sender.ID=m.sender
INNER JOIN users AS receiver ON receiver.ID=m.receiver
LEFT JOIN (SELECT mes.ID
FROM messages AS mes
WHERE mes.sender=1 OR mes.receiver=1
ORDER BY mes.date DESC LIMIT 1) AS convo
ON convo.ID=m.ID;
这样的事情会起作用吗?
如果你想看到我的完整sql:
(SELECT sender.ID AS senderID,sender.username AS senderUsername,sender.firstname AS senderFirstname,sender.lastname AS senderLastname,sender.othername AS senderOthername,sender.url_name AS senderUrl_name,senderI.path as sender_default_image,
receiver.ID AS receiverID,receiver.username AS receiverUsername,receiver.firstname AS receiverFirstname,receiver.lastname AS receiverLastname,receiver.othername AS reciverOthername,receiver.url_name AS recieverUrl_name,receiverI.path as reciever_default_image,
messages.message,messages.date,messages.read
FROM '.$wpdb->prefix.'fishy_messages AS messages
INNER JOIN '.$wpdb->prefix.'fishy_users AS sender ON sender.ID=messages.sender
LEFT JOIN '.$wpdb->prefix.'fishy_profile_default_image AS senderDP ON senderDP.userID=sender.ID
INNER JOIN '.$wpdb->prefix.'fishy_users AS receiver ON receiver.ID=messages.receiver
LEFT JOIN '.$wpdb->prefix.'fishy_profile_default_image AS receiverDP ON receiverDP.userID=receiver.ID
LEFT JOIN '.$wpdb->prefix.'fishy_images AS senderI ON senderI.ID=senderDP.imageID
LEFT JOIN '.$wpdb->prefix.'fishy_images AS receiverI ON receiverI.ID=receiverDP.imageID
WHERE messages.sender=%d OR messages.receiver=%d ORDER BY messages.date DESC LIMIT 1)
是的我使用wordpress:p
答案 0 :(得分:1)
要返回单个结果,在外部查询上给出Limit 1
,您已在INNER查询中给出。
SELECT sender.username,receiver.username,m.text,m.date FROM messages AS m
INNER JOIN users AS sender ON sender.ID=m.sender
INNER JOIN users AS receiver ON receiver.ID=m.receiver
LEFT JOIN (SELECT mes.ID
FROM messages AS mes
WHERE mes.sender=1 OR mes.receiver=1
ORDER BY mes.date DESC LIMIT 1) AS convo
ON convo.ID=m.ID GROUP BY convo.ID ;
答案 1 :(得分:0)
SELECT sender.ID AS senderID,sender.username AS senderUsername,sender.firstname AS senderFirstname,sender.lastname AS senderLastname,sender.url_name AS senderUrl_name,receiver.ID AS receiverID,receiver.username AS receiverUsername,receiver.firstname AS receiverFirstname,receiver.lastname AS recieverLastname,receiver.url_name AS recieverUrl_name,messages.message,messages.date,messages.read
FROM wp_6_fishy_messages AS messages
INNER JOIN wp_6_fishy_users AS sender ON sender.ID=messages.sender
INNER JOIN wp_6_fishy_users AS receiver ON receiver.ID=messages.receiver
WHERE messages.sender=1 OR messages.receiver=1
GROUP BY messages.sender,messages.receiver ORDER BY messages.date DESC
因为我在发送方和接收方之间没有conversation ID/key
作为我的对话。我把这两个人分组了!这给了我这些用户之间的最后两条消息:)