MySQL reference row in sub query
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
精彩评论