Mysql 查詢近半年的資料

苹果芒發表於2024-10-30
#查詢近半年的資料
select * 
from youTable
where time_colume>=curdate()- interval 6 month

#查詢近3年的資料
select * 
from youTable
where time_colume>=curdate()- interval 3 year

2.按月份彙總資料

#查詢近半年的資料,並且按照月份彙總
select concat(year(time_colume),month(time_colume)) as data, sum(amount) as total_amount
from youTable where time_colume>=curdate()- interval 6 month
group by month(time_column)


#查詢近3年的資料,
並且按照月份彙總
select concat(year(time_colume),month(time_colume))  as data, sum(amount) as total_amount
from youTable 
where time_colume>=curdate()- interval 3 year
group by month(time_column)

相關文章