我有一张产品表和一张价格表。价格定期更新。现在我想显示今天的价格,如果它今天没有更新,那么它会显示它的上一个日期价格。
我们如何使用单一查询执行此操作?
OP的评论中包含:
其实我有两张桌子
1)products
,2)product_price
和
我们每天在product_price
表中输入产品价格,如
+---------+-------+-------+
| Product | Price | Date |
+---------+-------+-------+
| A | 33k | 26Jan |
| B | 34.0k | 26Jan |
| B | 34.5k | 26Jan |
| A | 32k | 27Jan |
| A | 34k | 27Jan |
| C | 34.5 | 27Jan |
+---------+-------+-------+
预期输出:
+---------+-------+-------+
| Product | Price | Date |
+---------+-------+-------+
| A | 32k | 27Jan |
| B | 34.0k | 26Jan |
| C | 34.5k | 27Jan |
+---------+-------+-------+
答案 0 :(得分:0)
试试这个:
select
product, min( price ) as price, date as dt
from
product_prices
where
-- use the following if your date field is of type date
date <= curdate()
-- use the following if date field is in string format (26Jan)
-- date <= date_format( curdate(), '%m%b' )
group by
product;