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

前端 未结 4 1841
清歌不尽
清歌不尽 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:31

    It appears that it is impossible to use an ORDER BY on a GROUP BY summarisation. My fundamental logic is flawed. I will need to run the following subquery.

    SELECT `p`.*, `pp`.`price` FROM `products` `p` 
    LEFT JOIN (
        SELECT `price` FROM `product_price` ORDER BY `date_updated` DESC
    ) `pp` 
    ON `p`.`product_id` = `pp`.`product_id`
    GROUP BY `p`.`product_id`;
    

    This will take a performance hit but as it is the same subquery for each row it shouldn't be too bad.

提交回复
热议问题