I have to manage multiple price lists with different validity start date.
How can I select only the prices with the correct validity date from the price list table?
If an item has more than one price I must only take the most recent one but with a date no later than today! I hope I explained myself..
Normally I use the INNER JOIN but in this case I do not know how to select the data properly.
I need someting like this:
SELECT * FROM $item_table
LEFT JOIN $price_table ON $item_table.product_code = $price_table.product_code
WHERE ($price_table.validity_date /*is the most recent but no later than today*/ )
I give you an example of table structure:
Items table
+-------+--------------+-------------+
| Brand | Product Code | Description |
+-------+--------------+-------------+
| XXX | XXX-123 | Lorem ipsum |
| XXX | XXX-456 | Lorem ipsum |
| YYY | YYY-123 | Lorem ipsum |
| YYY | YYY-456 | Lorem ipsum |
| ZZZ | ZZZ-123 | Lorem ipsum |
+-------+--------------+-------------+
Price lists table
+-------+--------------+-------+---------------------+
| Brand | Product Code | Price | Validity Start Date |
+-------+--------------+-------+---------------------+
| XXX | XXX-123 | 10.00 | 2016/01/01 |
| YYY | YYY-123 | 20.00 | 2016/01/01 |
| XXX | XXX-123 | 12.00 | 2017/01/01 | <- I need this one
| YYY | YYY-123 | 22.00 | 2017/01/01 | <- I need this one
| XXX | XXX-123 | 15.00 | 2018/01/01 |
| YYY | YYY-123 | 25.00 | 2018/01/01 |
+-------+--------------+-------+---------------------+
Unfortunately, I can not put a price validity end date because when I put it in, I do not know when the next price list will be loaded. Otherwise, it would have been easier.
I accept any suggestion on a different way to structure the price lists table if this can lead to simpler solutions!
You need to find the one and only product that matches the following rules :
$brand
$code
MAX()
) of the previous matches So if I only wanted the price here's how I would formulate my query :
SELECT `Price`
FROM `Price lists`
WHERE `Brand` = :brand
AND `Product Code` = :productCode
AND `Validity Start Date` = (
SELECT MAX(`Validity Start Date`)
FROM `Price lists`
WHERE `Brand` = :brand
AND `Product Code` = :productCode
AND `Validity Start Date` < CURDATE()
)
EDIT : Added the filter on Brand and Product Code in the inner function
RE-EDIT : So if you need a full table of all products with the correct price in each line it gives the following query
SELECT a.*, b.Price, b.`Validity Start Date`
FROM Items a
LEFT JOIN (
SELECT `Product Code`, `Validity Start Date`, Price
FROM `Price lists`
WHERE `Validity Start Date` < CURDATE()
) b ON a.`Product Code` = b.`Product Code`
WHERE b.`Validity Start Date` = (
SELECT MAX(`Validity Start Date`)
FROM `Price lists`
WHERE `Product Code` = a.`Product Code`
AND `Validity Start Date` < CURDATE()
)
SELECT MIN(test.diff),test.* FROM (SELECT *,DATEDIFF(`Validity Start Date`,CURDATE())as diff FROM `Price_lists_table` LEFT JOIN $price_table ON $item_table.product_code = $price_table.product_code WHERE DATEDIFF(`Validity Start Date`,CURDATE()) >0 ) as test;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.