题解 | 获取指定客户每月的消费额
获取指定客户每月的消费额
https://www.nowcoder.com/practice/ed04f148b63e469e8f62e051d06a46f5
# 如果用到分组操作,select语句中的非聚合字段必须出现在group by中的 # 比如分组是month(t_time),select是substring_index(t_time,'-',2)也不行 # 此外还可以用date_format(t_time,'%Y-%m')来设置时间格式 select substring_index(t_time,'-',2) time, sum(t_amount) total from trade a inner join customer b on a.t_cus = b.c_id where c_name = 'Tom' and year(t_time)=2023 and t_type = 1 group by time order by time;
我是用substring_index取子串来完成题目时间格式要求的,此外还可以用date_format(t_time,'%Y-%m')来实现。