With this query:
SELECT
`id`,
`type`,
`subtype`,
`title`,
`shortdesc`,
(SELECT COUNT(*)
FROM `story_comments`
WHERE `parent_id` = t1.`id`) as comments,
(SELECT
(ROUND( (
SELECT (SUM(`rating` * `count`) / SUM(`count`) ) ) * 2) ) / 2 as result
FROM
(SELECT rating, COUNT(*) as count
FROM `story_ratings` WHERE `parent_id` = t1.`id`
GROUP BY rating) as val) as rating,
`calls`,
`user`
FROM
`storys` t1
WHERE
`open` = 1 AND
`modremove` = 0 AND
`modblock` = ''
ORDER BY
`opening`
DESC LIMIT 16;
I get this error: #1054 - Unknown column 't1.id' in 'where clause', which is caused by the subquery in the subquery (subquery after FROM).
But the t1.id
in the first subquery is working fine. Why cant I use it in the FROM-subquery? I also tried variables, which also didnt work:
SELECT @i := `id` id, `type`, `subtype`, `title`, `shortdesc`, (SELECT COUNT(*) FROM `story_comments` WHERE `parent_id` = t1.`id`) as comments,
(SELECT (ROUND( (SELECT (SUM(`rating` * `count`) / SUM(`count`) ) ) * 2) ) / 2 as result FROM (SELECT rating, COUNT(*) as count FROM `story_ratings` WHERE `parent_id` = @i GROUP BY rating) as val) as rating,
`calls`, `user` FROM `storys` t1 WHERE `open` = 1 AND `modremove` = 0 AND `modblock` = '' ORDER BY `opening` DESC LIMIT 16;
With the @i
variable, result returned NULL on every row, what is wrong.