失效语句:
获取的change_date不是最大值
select product_id, new_price, change_date
from (
select product_id ,new_price, change_date
from Products
order by change_date desc
) as a
group by product_id
生效语句:
增加distinct使order by生效
select product_id, new_price, change_date
from (
select distinct(product_id) product_id, new_price, change_date
from Products
order by change_date desc
) as a
group by product_id
文章讨论了在SQL查询中,不使用distinct时change_date排序失效的问题,以及加入distinct后如何确保正确按change_date降序排列产品信息。作者提供了两个SQL语句示例进行对比。
3563

被折叠的 条评论
为什么被折叠?



