嗨,我想问一下如何按特定 id 生成数据组并找到该 id 的最大值,
例如我有表学生测验
id student_id score
1 2 200
2 2 100
3 3 300
4 3 200
我想要这样的输出:[
{
id: 1,
student_id: 2,
score: 200
},
{
id: 3,
student_id: 3,
score: 300
}
]
我如何使用 sequelize 制作它?提前致谢
最佳答案
我假设您已经创建了正确的 Sequelize 模型,请尝试使用以下代码片段。
StudentQuiz.findAll({
attributes: [Sequelize.fn('max', Sequelize.col('score'))],
group: ["student_id"],
raw: true,
})
关于node.js - 如何在 Sequelize.js 中查询 Group by 和 MAX(id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62796203/