I've a product and a price table (with product id, price and timestamp columns). Now I need to join to a simple select of products the newest price.
The simple join will select the first saved price in the table but not the newest.
SELECT p.*, pp.price
FROM products p
INNER JOIN product_prices pp ON (pp.product_id = p.id)
So I've tried to join with select the newest timestamp, but this will only return the newest timestamp for all records and not per product_price:
SELECT p.*, pp.*
FROM products p
LEFT JOIN
(SELECT product_id, ranking, MAX(timestamp) as timestamp
FROM product_prices) pp
ON pp.product_id = p.id
Is there an smart/right way to select all products with the newest price per product?