Javascript 如何使用sequelize或sql从不同的表聚合总和?

Javascript 如何使用sequelize或sql从不同的表聚合总和?,javascript,sql,node.js,sequelize.js,Javascript,Sql,Node.js,Sequelize.js,我有三个相互关联的模型: 锻炼 电路 电路步 每个电路都有一个repeatCount,它重复了多少次,每个电路步骤都有一个持续时间 我如何告诉训练模型使用电路步骤.持续时间和电路.重复计数计算持续时间的总和?这种聚合可能吗 //models/Workout.js module.exports=(sequelize,数据类型)=>{ 常量训练=sequelize.define( “锻炼”, { 名称:{type:DATATYPES.STRING}, 持续时间:{type:DATATYP

我有三个相互关联的模型:

  • 锻炼
    • 电路
      • 电路步
每个电路都有一个
repeatCount
,它重复了多少次,每个电路步骤都有一个
持续时间

我如何告诉
训练
模型使用
电路步骤.持续时间
电路.重复计数
计算
持续时间
的总和?这种聚合可能吗

//models/Workout.js
module.exports=(sequelize,数据类型)=>{
常量训练=sequelize.define(
“锻炼”,
{
名称:{type:DATATYPES.STRING},
持续时间:{type:DATATYPES.STRING},
}
);
Workout.associate=功能(型号){
模型。锻炼。有很多(模型。电路);
};
回程训练;
};
//models/Circuit.js
module.exports=(sequelize,数据类型)=>{
const Circuit=sequelize.define(
“电路”,
{
姓名:{
类型:DATATYPES.STRING,
allowNull:错误
},
训练计划编号:{
类型:DATATYPES.INTEGER,
allowNull:错误
},
重复计数:{
类型:DATATYPES.INTEGER,
allowNull:错误
},
},
);
Circuit.associate=功能(型号){
models.Circuit.hasMany(models.CircuitStep);
模型。电路。归属(模型。训练);
};
回路;
};
module.exports=(续集,数据类型)=>{
const CircuitStep=sequelize.define(
“电路步骤”,
{
持续时间:{
类型:DATATYPES.INTEGER
},
练习编号:{
类型:DATATYPES.INTEGER
},
线路标识:{
类型:DATATYPES.INTEGER
},
}
);
CircuitStep.associate=函数(模型){
models.Circuit步骤.belongsTo(models.Circuit);
};
返回电路步骤;
};

是的,聚合是绝对可能的。您可以结合使用
sequelize.fn
(SUM)和
sequelize.col
,但由于您需要从三个级别访问数据,因此最好使用
sequelize.literal
并传入原始列select查询。您可以通过以下方式实现这一点:

const workout = await Workout.findAll({
    // The query will refer to Workout model as `workout`
    attributes: [

        // Notice how I referred to the attributes using the `tableNames` in backticks becuase `->` is
        // an invalid character in an SQL query but sequelize when generating the SQL query assigns
        // this alias to the nested included tables
        [sequelize.literal('SUM(`circuits`.duration * `circuit->circuitSteps`.repeatCount'), 'total']
    ],
    include: [
        {
            // This will join Workout to Circuit table
            // The query generated by sequelize will refer to Circuit as `circuits`
            // since Workout.hasMany(Circuit)
            model: Circuit,
            attributes: [],

            // This will join Circuit to CircuitStep
            // The query generated will refer to CircuitStep as `circuit->circuitSteps` 
            // If not working then log the query and the alias in the SQL query for the table
            include: [
                {
                    model: CircuitStep,

                    // You can fetch any attributes you want I prefer aggregating everything on
                    // top level using sequelize.literal
                    attributes: []
                }
            ]
        }
    ],
    group: ['workout.id'],
})

如果您需要进一步帮助,请留下评论。这对我来说将是一种乐趣,因为我自己已经在这个问题上纠缠了很长时间。

是的,聚合是绝对可能的。您可以结合使用
sequelize.fn
(SUM)和
sequelize.col
,但由于您需要从三个级别访问数据,因此最好使用
sequelize.literal
并传入原始列select查询。您可以通过以下方式实现这一点:

const workout = await Workout.findAll({
    // The query will refer to Workout model as `workout`
    attributes: [

        // Notice how I referred to the attributes using the `tableNames` in backticks becuase `->` is
        // an invalid character in an SQL query but sequelize when generating the SQL query assigns
        // this alias to the nested included tables
        [sequelize.literal('SUM(`circuits`.duration * `circuit->circuitSteps`.repeatCount'), 'total']
    ],
    include: [
        {
            // This will join Workout to Circuit table
            // The query generated by sequelize will refer to Circuit as `circuits`
            // since Workout.hasMany(Circuit)
            model: Circuit,
            attributes: [],

            // This will join Circuit to CircuitStep
            // The query generated will refer to CircuitStep as `circuit->circuitSteps` 
            // If not working then log the query and the alias in the SQL query for the table
            include: [
                {
                    model: CircuitStep,

                    // You can fetch any attributes you want I prefer aggregating everything on
                    // top level using sequelize.literal
                    attributes: []
                }
            ]
        }
    ],
    group: ['workout.id'],
})

如果您需要进一步帮助,请留下评论。这对我来说是一件很高兴的事,因为我自己也在这个问题上纠缠了很长时间。

[[sequelize.literal('SUM….我想你必须去掉一个开放的方括号,它是关于结束的bracket@Tharzeezremovedattributes:[],非常重要[[sequelize.literal](‘SUM…’我想你必须去掉一个开放的方括号,这是关于结束的bracket@Tharzeezremovedattributes:[],非常重要