场景
一个 chat_channel 可以有多个 MESSAGE_ID,它们显示在不同的行上。
INSERT INTO messages_tbl
(message_id,
chat_channel,
user_type,
message_type,
message_custom_type,
msg_created_on)
VALUES ('00001',
'chat_1',
'consumer',
'msg',
'example message',
'2018-09-27 10:14:42');
INSERT INTO messages_tbl
(message_id,
chat_channel,
user_type,
message_type,
message_custom_type,
msg_created_on)
VALUES ('00002',
'chat_1',
'merchant',
'msg',
'example message',
'2018-09-27 11:14:42');
在这个例子中,我想比较和计算从消费者发送的第一条消息到商家发送的第二条消息的响应时间。有关时间和日期的数据是 MSG_CREATED_ON 列。
我能够进行计算,但因为他们在不同的行上而苦苦挣扎。我曾尝试在 CHAT_CHANNEL 上对它们进行分区,但没有成功..
有什么想法吗?
最佳答案
下面是一种方式-
SELECT t1.chat_channel,
Timestampdiff(minute, t1.msgcreated_on, t2.messagecreated_on)
FROM (SELECT chat_channel,
Min(msgcreated_on) time1
FROM message_tbl
WHERE t1.user_type = 'CONSUMER'
GROUP BY chat_channel) t1
INNER JOIN (SELECT chat_channel,
Min(msgcreated_on) time2
FROM message_tbl
WHERE t1.user_type = 'MERHCANT'
GROUP BY chat_channel) t2
ON t1.chatchannel = t2.chatchannel
关于mysql - 同一聊天中两条消息的响应时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54805390/