This is your homework and you should attempt it yourself.
what-have-you-tried[
^]
If you meet with any problems with your code, always ask google first, then visit CP, show your code and ask questions related to specific issue.
You may refer to
php_mysql[
^] for help.
+++++++++++++++++++++++++++++++
Ok, since you insisted, I will try to shed some light on question 3 which appeared to be the hardest:
First you got to find the prices of the last datetime and second last datetime respectively. To find the last datetime is easy, just use MAX(date). How about the second last datetime, again use MAX(date) but this time excludes the last datetime from your SELECT pool.
The rest is just arithmetic.
Study my sql statement below and figure out the logic yourself. I have used local variables to make the code cleaner and reusable.
SET @product_id = 1;
SET @last_date = (SELECT MAX(date) FROM product WHERE product_id = @product_id);
SET @second_last_date = (SELECT MAX(date) FROM product WHERE product_id = @product_id
AND date < @last_date);
SELECT
(
(SELECT price FROM product WHERE date = @last_date AND product_id = @product_id)
-
(SELECT price FROM product WHERE date = @second_last_date AND product_id = @product_id)
) AS PRICE_DIFFERENCE
If you can figure out my solution to question 3, you have no problem solving questions 1 and 2. My code will simply find the price difference between last date and second last date so it will result in negative value if last price is lower than the second last price. Good luck.