根据用户ID查询某一天的消费汇总
//time为字段,字段类型为int的使用这个
select uid,SUM(price) as prices from xiaofei where FROM_UNIXTIME(time,'%Y-%m-%d') = '2021-08-08' GROUP BY uid
//字段类型为 timestamp 或日期格式的(1970-01-01 00:00:00)使用这个
select uid,SUM(price) as prices from xiaofei where DATE_FORMAT(time,'%Y-%m-%d') = '2021-08-08' GROUP BY uid
根据用户ID查询某一个月的消费汇总
//time 为字段,字段类型为int的使用这个
select uid,SUM(price) as prices from xiaofei where FROM_UNIXTIME(time,'%Y-%m') = '2021-08' GROUP BY uid
//字段类型为 timestamp 或日期格式的使用这个
select uid,SUM(price) as prices from xiaofei where DATE_FORMAT(time,'%Y-%m') = '2021-08' GROUP BY uid
根据用户ID查询某一年的消费汇总
//time 为字段,字段类型为int的使用这个
select uid,SUM(price) as prices from xiaofei where FROM_UNIXTIME(time,'%Y') = '2021' GROUP BY uid
//字段类型为 timestamp 或日期格式的使用这个
select uid,SUM(price) as prices from xiaofei where DATE_FORMAT(time,'%Y') = '2021' GROUP BY uid
根据用户ID查询某一小时的消费汇总
//time 为字段,字段类型为int的使用这个
select uid,SUM(price) as prices from xiaofei where FROM_UNIXTIME(time,'%H') = '11' GROUP BY uid
//字段类型为 timestamp 或日期格式的使用这个
select uid,SUM(price) as prices from xiaofei where DATE_FORMAT(time,'%H') = '11' GROUP BY uid
SUM 是MySQL返回指定字段的总和;
更多函数请看这里: