Sequelize.js中如何查询Group by和MAX(id)
How to query Group by and MAX(id) in the Sequelize.js
你好我想问一下如何根据某个id生成数据组并找到该id的最大值,
例如我有 Table 学生测验
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 制作它?提前致谢
我假设您已经创建了正确的续集模型,请尝试使用以下代码片段。
StudentQuiz.findAll({
attributes: [Sequelize.fn('max', Sequelize.col('score'))],
group: ["student_id"],
raw: true,
})
你好我想问一下如何根据某个id生成数据组并找到该id的最大值,
例如我有 Table 学生测验
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 制作它?提前致谢
我假设您已经创建了正确的续集模型,请尝试使用以下代码片段。
StudentQuiz.findAll({
attributes: [Sequelize.fn('max', Sequelize.col('score'))],
group: ["student_id"],
raw: true,
})