MySQL 根据用户ID查询某一天的消费汇总,以及按照指定时间查询数据

根据用户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返回指定字段的总和;

更多函数请看这里:

MySQL 函数 | 菜鸟教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值