douwei2825 2019-04-20 16:28
浏览 34

sql语句的问题

I have 2 tables

chat_rooms 
chat_name | chat_url | chat_id


chat_msgs
msg_chat_id | timestamp

I'm trying to display it like this :

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

Please help

  • 写回答

1条回答 默认 最新

  • dongshukou0240 2019-04-20 16:53
    关注

    Left join the messages to the chats on the chat ID and filtered for the last 10 minutes using timestampadd(). Then group by the chat room taking the count of timestamps.

    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;
    
    评论

报告相同问题?