Hello StackOverflow community,
I have 2 tables
products
=======
id
name
prices
======
id
product_id
price
date_added
The column ‘date_added’ is of DATETIME type.
My question is how can I select the products.name, prices.price, prices.date_added where the date_added is maximum.
The result set should contain 1 row per product with the price and the max date_added.
Please let me know if this is not understandable enough!
HERE IS A SET OF RECORDS
INSERT INTO `prices` (`id`, `product_id`, `price`, `date_added`) VALUES
(1, 1, 1.23, '2011-11-16 13:18:14'),
(2, 1, 1.25, '2011-11-17 13:18:24'),
(3, 2, 1.5, '2011-11-15 13:18:32'),
(4, 3, 1.89, '2011-11-15 13:18:41'),
(5, 4, 0.98, '2011-11-14 13:18:57'),
(6, 5, 1.87, '2011-11-17 13:19:29'),
(7, 5, 1.98, '2011-11-16 13:19:44'),
(8, 3, 1.87, '2011-11-13 13:19:52'),
(9, 2, 1.45, '2011-11-05 13:20:01'),
(10, 1, 2.34, '2011-11-16 13:20:10'),
(11, 4, 2.87, '2011-11-18 13:49:21'),
(12, 5, 1.64, '2011-11-19 13:49:33');
INSERT INTO `products` (`id`, `name`) VALUES
(1, 'Karamhtros'),
(2, 'Tralalas'),
(3, 'Giwrgos'),
(4, 'Sotiris'),
(5, 'Tassos');
Try this
EDITED after user comment:
Try this one