力扣SQL Top50: 指定日期的产品价格

1164. 指定日期的产品价格

1164. 指定日期的产品价格

left joinp2表的要判断当新价格为 null 的时候,要给它一个默认的初始价格10,因为原题表明,每个产品的初始价格就是10

where in用于where 子句中指定多个可能值。可以看做是多个or的简写形式,并且in会自动处理重复值

select p1.product_id, IFNULL(p2.new_price, 10) as price 
from Products p1
left join (
    select distinct product_id, new_price
    from Products
    where (product_id, change_date) in (
        select product_id, max(change_date)
        from Products
        where change_date <= '2019-08-16'
        group by product_id
    )
) as p2 
on p1.product_id = p2.product_id
group by p1.product_id;
posted @ 2025-09-19 22:41  Lantz12  阅读(12)  评论(0)    收藏  举报