Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i work with PHP And MySQL

I have two table :

Products (product_id,product_name)

Prices (product_id,price_id,price,date)

Any product have unlimited price

Type of “date” field in table “Prices” is datetime (like 2014-02-23 18:41:22) – when I saving any record current datetime saved in this field (last record can be determine with this field) .

I want a loop for all products that can do below actions for any product :

1)get Last date from prices table that related with current product and set it to $last_Date


for example if current product_id is 1 get date of last record in price table that product_id=1 and date=$last_Date
2)get Last price from prices table that related with current product


for example if current product id is 1 get price of last record in price table that product_id=1 and date=$last_Date

3)get different between field price from last 2 record


for example if current product id is 1 get different between field price of last record 2 record in price table that product_id=1 and date=$last_Date

4)get min and max *price* field value that product_id=1 and date=$last_Date
Posted

1 solution

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.
SQL
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.
 
Share this answer
 
v5
Comments
[no name] 16-Feb-14 1:38am    
i alredy search and try for multi time , but i cant do it .
please help
for sample see this image
http://upload7.ir/imgs/2014-02/43697365019310026073.jpg
thanks a lot
Peter Leow 16-Feb-14 3:23am    
see additional input in solution 1. Consider accepting it as solution and vote.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900