Hi, I have written an SQL query that gets unread messages but I think I can improve the code (speed and readability). The first select is for COUNT function, the second is for grouping messages according to conversation_id, and the final nested select is for selecting last messages.
Please give me suggestions. Thanks in advance.
SELECT COUNT(*) as unreaded FROM (
SELECT id
FROM (
SELECT id, conversation_id
FROM messages
WHERE to_id = ?
AND readed = 0
and NOT hide_from = ?
ORDER BY sended DESC
) AS temp_messages
GROUP BY conversation_id
) as temp_messages2