I have two tables
1: itemInfo
product_id brand name category
1 Garden Goodcake cake
2: itemRecord
record_id shopType price inputTime product_id
1 0 7.2 2012-03-20 17:58:56 1
69 2 6.4 2012-03-20 19:33:12 1
70 0 9.9 2012-03-19 00:00:00 1
71 3 5.5 2012-03-18 05:22:00 1
72 0 6.8 2012-03-19 20:26:29 1
73 0 10.1 2012-03-17 12:27:00 1
74 3 7.9 2012-03-16 00:00:00 1
75 1 6.7 2012-03-15 00:00:00 1
76 1 8.8 2012-03-14 11:24:00 1
77 2 12.5 2012-03-13 10:26:19 1
78 0 7.2 2012-03-21 11:25:23 1
146 2 5 2012-03-21 12:53:04 1
149 3 4.9 2012-03-21 10:00:00 1
150 3 5.1 2012-03-21 12:20:00 1
I would like to display the latest 7 days of cheapest price of product_id = 1.
If the same day, the cheapest price is same, just display the latest.
The final table should be:
product_id brand name category inputTime shopType minimum_price record_id
1 Garden Goodcake cake 2012-03-21 3 4.9 149
1 Garden Goodcake cake 2012-03-20 2 6.4 69
1 Garden Goodcake cake 2012-03-19 2 6.8 72
...
1 Garden Goodcake cake 2012-03-15 1 6.7 75
I have tried my best to write a sql that like:
SELECT r.product_id, i.brand, i.name, i.category, DATE_FORMAT( r.inputTime, '%e-%b' ) AS inputTime, r.shopType, MIN( r.price ) AS minimum_price, r.record_id
FROM itemRecord AS r, itemInfo AS i
WHERE r.product_id = '1'
AND i.id = r.product_id
GROUP BY DATE( r.inputTime ) DESC
LIMIT 0 , 7
However, the table is:
product_id brand name category inputTime shopType minimum_price record_id
1 Garden Goodcake cake 21-Mar 0 4.9 78
1 Garden Goodcake cake 20-Mar 0 6.4 1
1 Garden Goodcake cake 19-Mar 0 6.8 70
1 Garden Goodcake cake 18-Mar 3 5.5 71
1 Garden Goodcake cake 17-Mar 0 10.1 73
1 Garden Goodcake cake 16-Mar 3 7.9 74
1 Garden Goodcake cake 15-Mar 1 6.7 75
For example, the first row result, it displays the correct minimum price 4.9, but the record_id is wrong, also, the shopType is wrong too.
I have research a lot, but still cannot solve the problem.
Please help...