with cu as (
    select * 
    from trade
    where year(t_time)='2023' and t_type='1'
)
select DATE_FORMAT(t_time, '%Y-%m') as time,sum(t_amount) as total
from cu t left join customer c on t.t_cus=c.c_id
where c.c_name='Tom'
group by DATE_FORMAT(t_time, '%Y-%m')
order by DATE_FORMAT(t_time, '%Y-%m') 
首先从trade表中筛选出2023年, 消费的数据,也就是year(t_time)='2023' and t_type='1'
其次很明显,是要按照 月份 分组,分完后就是一个月对应多个消费数据, 并且使用sum 来统计每个月消费金额。
这里使用inner join和left join应该没有啥区别。

京公网安备 11010502036488号