在我的数据库中,我存储了每天的产品价格。
我需要基于每月最后记录的记录。
当我按月分组申请时,它给出了该月的第一条记录。
我尝试过的查询是,
select * from product where product_id = 52
AND YEAR(date_modified) = YEAR(NOW()) GROUP BY MONTH(date_modified)
欢迎任何想法
最佳答案
有一个派生表,您可以在其中使用GROUP BY
查找每个产品的每月最新日期。加入该派生表:
select * from
product t1
join (select product_id, max(date_modified) max_date_modified from product
group by product_id, YEAR(date_modified), MONTH(date_modified)) t2
on t1.product_id = t2.product_id and t1.date_modified = t2.max_date_modified
where product_id = 52
关于mysql - 获取每个月的最后一条记录 - mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35331315/