UPDATE messages, (SELECT messages.`message_id` midd, MAX(COALESCE(replies.ts, messages.ts)) lastt  FROM messages LEFT JOIN replies ON messages.`message_id`=replies.`message_id` GROUP BY messages.`message_id`) res
SET messages.updated = res.lastt WHERE messages.`message_id` = res.midd ;