SELECT
messages.message
FROM
messages
JOIN (
SELECT
MAX(date) AS last_message, dialog_id
FROM
messages
WHERE
from_user_id = 15 OR to_user_id = 15
GROUP BY
dialog_id
) tmp ON messages.dialog_id = tmp.dialog_id AND messages.date = tmp.last_message
WHERE
messages.from_user_id = 15 OR messages.to_user_id = 15