【发布时间】:2019-11-10 22:05:34
【问题描述】:
我想知道如何对我的环回查询进行聚合我使用 MySQL 作为数据库我在我的数据库中有这样的记录 -
{ 'app_name': 'chrome', 'duration' : 10000 }, { 'app_name': 'WhatsApp', 'duration' : 25000 } and so on
请注意,持续时间以毫秒为单位。我正在使用 Angular 7 作为前端,我想进行环回查询来总结我所有记录的持续时间,现在我正在做一个类似的查询 -
Apps.find({}).toPromise()
.then(apps => {
let result = apps.reduce((app, total) = {
total += app.duration;
return total
}, 0)
console.log(result);
})
.catch(err => {
console.log(err);
})
但是,使用这种方法,我可以获得持续时间的总和,但如果我有数百万条记录,那么它不是一个可扩展的解决方案,例如从数百万条记录中迭代然后对持续时间求和。 我想知道 MySQL 的环回查询中是否存在聚合。 我想要一个类似的查询 -
Apps.find({
aggregation: {
sum: 'duration'
}
}).toPromise()
.then(result => {
console.log(result);
})
.catch(err => {
console.log(err);
})
类似的东西。
【问题讨论】: