如何为以下任务制定查询:
假设您以用户身份登录:1 我想在每次对话中获得一行。 对于我想要获取的每一行, 对话中第一行的“主题” 第一行的“DateTime” "Message"本次对话的最后一条消息,无论是谁写的
CREATE TABLE messages ( ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, FromID INT NOT NULL, ToID INT NOT NULL, ConversationID INT NOT NULL, Subject varchar(255), Message varchar(255), DateTime DATETIME ) ENGINE=InnoDB; CREATE TABLE conversations ( ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE=InnoDB; INSERT INTO conversations (ID) VALUES (1), (2), (3); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (1,2, 1, "Hi", "This is a test message", "2010-08-08 16:23:48"); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (1,2, 1, "", "Hey again you have not answered", "2010-08-08 16:23:52"); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (2,1, 1, "", "Hi this is my answer", "2010-08-08 16:23:59"); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (2,1, 2, "2.Hi", "2.This is a test message", "2010-08-08 16:25:48"); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (1,2, 2, "", "2.Hi back", "2010-08-08 16:25:52"); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (2,1, 2, "", "2.Hi this is my answer", "2010-08-08 16:25:59"); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (2,1, 3, "3.Hi", "3.This is a test message", "2010-08-08 16:27:48"); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (1,2, 3, "", "2.Hi back", "2010-08-08 16:27:52"); INSERT INTO messages (FromID, ToID, ConversationID, Subject, Message, DateTime) VALUES (1,2, 3, "", "2.Hello are you there?", "2010-08-08 16:27:59");
最佳答案
SELECT M.ConversationID,
MAX(CASE WHEN M.DateTime = X.FirstRow THEN M.Subject END) AS Subject,
CAST(COALESCE(MAX(CASE WHEN M.DateTime = X.LastRowSentByOtherUser
THEN M.DateTime END),X.LastRow) AS DateTime)AS LastTime,
MAX(CASE WHEN M.DateTime = X.LastRow THEN M.Message END) AS Message,
MAX(CASE WHEN FromID = 1 THEN ToID ELSE FromID END) AS OtherParticipantId
FROM messages M
JOIN (
SELECT ConversationID, MIN(DateTime) AS FirstRow, MAX(DateTime) AS LastRow,
MAX(CASE WHEN FromID<>1 THEN DateTime END) AS LastRowSentByOtherUser
FROM messages
WHERE FromID=1 OR ToID=1
GROUP BY ConversationID
) X ON X.ConversationID = M.ConversationID
AND (M.DateTime IN (X.FirstRow, X.LastRow, X.LastRowSentByOtherUser))
GROUP BY M.ConversationID
HAVING MAX(CASE WHEN M.DateTime = X.LastRowSentByOtherUser
THEN M.DateTime END) IS NOT NULL
关于sql - 获取每个组的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3435957/