且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL LEFT JOIN,GROUP BY和ORDER BY无法按要求工作

更新时间:2022-05-17 02:50:59

在GROUP BY摘要上使用ORDER BY似乎是不可能.我的基本逻辑是有缺陷的.我将需要运行以下子查询.

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.