I'm working with a currency rate DB in which there are rates for multiple currencies fetched from multiple sources.
I'm trying to build a query which fetches the last two records for each currency from each source. In total there are about 60 currencies fetched from 10 different sources.
SELECT c.id, c.name, c.iso4217, c.flag,
r1.buy, r1.sell, r1.amount, r1.time
FROM currencies c
JOIN rates r1 ON r1.currency = c.id
JOIN src s ON s.id = r1.src
WHERE c.status = 10
AND s.status = 10
AND r1.id IN (
SELECT id
FROM
(
SELECT r2.id
FROM
rates r2
WHERE
r2.currency = c.id
AND r2.src = s.id
ORDER BY r2.id
DESC LIMIT 2
)alias
)
This query won't run and just ends up returning
Unknown column 'c.id' in 'where clause'
It does work perfectly fine and giving me the data I want if I manually sets
r2.currency = #
AND r2.src = #
to any ids in the innermost subquery.
How can I pass c.id and s.id to the subquery?