sql语句的问题

时间:2019-04-20 16:28:59

标签: php sql

我有2张桌子

chat_rooms 
chat_name | chat_url | chat_id


chat_msgs
msg_chat_id | timestamp

我正试图像这样显示它:

-------- chat rooms that were active last 10 minutes---------
chat_name | chat_url | count the messages 

请帮助

1 个答案:

答案 0 :(得分:2)

将消息加入聊天ID上的聊天中,并使用timestampadd()过滤最近10分钟。然后按聊天室分组以获取时间戳记。

SELECT cr.chat_name,
       cr.chat_url,
       count(cm.timestamp) "count the messages"
       FROM chat_rooms cr
            LEFT JOIN chat_msgs cm
                      ON cm.msg_chat_id = cr.chat_id
                         AND cm.timestamp >= timestampadd(minute, -10, now())
                         AND cm.timestamp < now()
       GROUP BY cr.chat_name,
                cr.chat_url;