count(*) from messages
where chat_id='b9cc4b56-0f9c-11e9-aca3-93f8807f4b84'
)
- (
select count(*) from messages
left join messages_users
on messages.id = messages_users.message_id
where chat_id='b9cc4b56-0f9c-11e9-aca3-93f8807f4b84'
and recipient_id='c15a638c-0f9a-11e9-860d-93edbd5319f8'
read_at is null
) as total_count_unread_messages;
messages(id, chat_id, author_id, text)
messages_users(message_id, recipient_id, read_at)
Может быть Вам помогут оконные функции? count (*) over (partition by chat_id) - count (*) over (partition by chat_id, recipient_id)
Обсуждают сегодня