在我的数据库中,我已经存储了每天的产品价格。
我需要根据每个月的最后记录记录。
当我按月申请时,它会给出当月的第一个记录。
我试过的查询是,
select * from product where product_id = 52
AND YEAR(date_modified) = YEAR(NOW()) GROUP BY MONTH(date_modified)
欢迎任何想法
答案 0 :(得分:1)
有一个派生表,您使用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