题解 | 获取指定客户每月的消费额
获取指定客户每月的消费额
https://www.nowcoder.com/practice/ed04f148b63e469e8f62e051d06a46f5
#查询 Tom 2023年 每月 消费金额 月份正序排列,输出 年月 消费金额 #时间挑出年和月,用客户表id匹配出金额并用月份分组,金额保留一位小数 select date_format(t_time,'%Y-%m') as time, #date_format() 日期格式化函数,其中%Y表示四位数的年份,%m表示两位数的月份。as 'time'表示给这一列起一个别名叫'time'。注意:别名可以使用单引号、双引号或者不用引号,但在某些情况下(比如别名包含特殊字符或空格)必须使用引号。 round(sum(t_amount),1) as total #round:保留小数位数1 ,sum :匹配出的Tom消费金额求和 from customer c join trade t on c.c_id=t.t_cus #使用客户id匹配出消费id,这里注意c.id不等于t.id where year(t_time) = '2023' and t.t_type=1 and c.c_name = 'Tom' #查找账单中年为2023,名字为Tom的消费条目 group by time #按月份分组 order by time #按月份排序,默认正序
查看11道真题和解析