开发者

MySQL reference row in sub query

开发者 https://www.devze.com 2023-03-17 00:08 出处:网络
I\'m trying to count all messages sent by users AFTER they uploaded a photo. I\'m trying something like this.

I'm trying to count all messages sent by users AFTER they uploaded a photo.

I'm trying something like this.

select messages.created_at, count(*) as count from messages 
      inner join users on messages.user_id = users.id
      inner join photos on photos.user_id = users.id
      where 
        some_users_messages.created_at > some_users_first_photo.created_at
      group by YEARWEEK(messages.created_at)

I'm thinking this needs to be a subquery? I'm not sure how to do this 开发者_Python百科concept of one particular user's messages/photos in MySQL. Any ideas?

Thanks!


This would count the number of messages sent after the first photo per user:

select  messages.user_id
,       count(*) as count 
from    messages 
where   messages.created_at >
        (
        select  min(created_at)
        from    photos
        where   photos.user_id = messages.user_id
        )
group by 
        messages.user_id
0

精彩评论

暂无评论...
验证码 换一张
取 消