I have parent posts table and child votes with posts.id and votes.post_id relation. I want to count average rating for each post but for only latest given 50 votes. I know how to do that for all votes:
SELECT T1.`title`, (
SELECT AVG(`vote`)
FROM `votes`
WHERE `votes`.`post_id` = T1.`id`
) AS `average`
FROM `posts` T1
GROUP BY T1.`id`
I know this possible to do with subquery:
SELECT T1.`title`, (
SELECT AVG(`vote`)
FROM (
SELECT `vote` FROM `votes`
WHERE `votes`.`post_id` = T1.`id`
ORDER BY `votes`.`id` DESC
LIMIT 10
) AS T2
) AS `average`
FROM `posts` T1
GROUP BY T1.`id`
But there is error: Error in query (1054): Unknown column 'T1.id' in 'where clause'. T1 alias is not accessible in subsubquery. Any ideas?