SELECT d.*, (SELECT COUNT(1) FROM dialog_messages AS dm WHERE NOT EXISTS (SELECT * FROM message_views AS mv WHERE mv.message_id = dm.id AND user_id = <ПОЛЬЗОВАТЕЛЬ> ) WHERE dm.dialog_id = d.id ) AS messageCount
FROM dialogs AS d
INNER JOIN dialogs_users AS du ON du.dialog_id = d.id
WHERE user_id = <ПОЛЬЗОВАТЕЛЬ>