Answer the question
In order to leave comments, you need to log in
How to get the item with the latest price?
Conditions: there is a Product and there is a ManyToOne Price (value, created_at, product_id)
How to put together a query in a query builder that will display products with a price (the last one according to created_at)? On bare SQL, it looks like this
SELECT *,
(
SELECT id FROM price
WHERE price.product_id = product.id
ORDER BY created
DESC LIMIT 1
)
FROM product
Answer the question
In order to leave comments, you need to log in
Related answer:
SELECT product.*, last_prices.*
FROM product
LEFT JOIN (
SELECT product_id AS id, max(id) as price_id
FROM price
GROUP BY product_id
) last_prices ON last_prices.id=product.id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question