如何解决如何从一年中按月分组的mysql中选择并在一个月内求和值?
var cmdmap = {
role: gimmerole
};
function gimmerole(message,args) {
const member = message.member;
const role = message.guild.roles.cache.find(r => r.name === args.join(' '));
if (!role) return console.log(`The role "${args.join(' ')}" does not exist`);
member.roles.add(role);
}
client.on('message',(msg) => {
var cont = msg.content,author = msg.member,chan = msg.channel,guild = msg.guild;
if (author.id !== client.user.id && cont.startsWith(config.prefix)) {
const [invoke,...args] = cont.slice(config.prefix.length).trim().split(' ');
console.log(invoke,args);
if (invoke in cmdmap) {
cmdmap[invoke](msg,args);
}
}
})
现在,当用户从日历中选择年份时,我想在我的 PHP 网页中显示类似于下面提到的表格数据的内容(为此我使用了 Zebra 日期选择器)。 Zebra 日期选择器给出年份。
received_amount | updated_at
----------------------------
4000 | 2020-11-02
2000 | 2020-11-03
1000 | 2020-12-02
1000 | 2020-12-04
3000 | 2021-01-02
2000 | 2020-01-02
解决方法
您可以在查询中像这样使用 group by
GROUP BY EXTRACT(YEAR FROM updated_at),EXTRACT(MONTH FROM updated_at)
然后像这样选择
SELECT EXTRACT(YEAR FROM updated_at) as Year,EXTRACT(MONTH FROM updated_at) as Month,SUM(received_amount) as Monthly_Total
,
你可以这样做:
select year,month,monthly_total
from (
select
extract(year from updated_at) as year,extract(month from updated_at) as m,monthname(updated_at) as month,sum(receive_amount) as monthly_total
from t
where extract(year from updated_at) = 2020
group by year,m
) x
order by year,m
结果:
year month monthly_total
----- --------- -------------
2020 January 2000
2020 November 6000
2020 December 2000
请参阅 DB Fiddle 上的运行示例。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。