MySQL LEFT JOIN, GROUP BY and ORDER BY not working as required

前端 未结 4 1834
清歌不尽
清歌不尽 2020-12-16 17:18

I have a table

\'products\' => (\'product_id\', \'name\', \'description\') 

and a table

\'product_price\' => (\'pro         


        
4条回答
  •  温柔的废话
    2020-12-16 17:30

    This will give you the last updated price:

    select
      p.*, pp.price
    from
      products p,
      -- left join this if products may not have an entry in prodcuts_price
      -- and you would like to see a null price with the product
      join 
      (
          select 
            product_price_id, 
            max(date_updated) 
          from products_price
          group by product_price_id
       ) as pp_max
        on p.product_id = pp.product_id
      join products_price pp on 
        pp_max.prodcuts_price_id = pp.products_price_id
    

提交回复
热议问题