I have an 1:m association.
symbols table has many rows in company_key_statistics table. company_key_statistics has column createdAt which is timestamp indicating when row has been created.
I need to join latest symbols with company_key_statistics, but I need only latest company_key_statistics.
For example I need to get ORCL and MSFT symbols and only their latest company_key_statistics.
So far I have tried this.
SELECT `symbols`.`id`,
`symbols`.`symbol`,
`statistics`.
`marketCapitalization`
FROM `symbols`
LEFT JOIN (SELECT `s`.`companyId`,
`s`.`marketCapitalization`
FROM `company_key_statistics` AS `s`
WHERE `s`.`companyId` = `symbols`.`id`
ORDER by `createdAt`
DESC LIMIT 1) AS `statistics`
ON `symbols`.`id` = `statistics`.`companyId`
WHERE `symbols`.`symbol` IN ('ORCL', 'SNAP');
But unfortunately I found that I can't use columns from parent query in JOIN subqueries.
How can I achieve to this?