提问者:小点点

如何通过Sequeize生成数据


你好,我想问一下如何产生数据组按某些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生成它? 提前致谢


共1个答案

匿名用户

我假设您已经创建了正确的sequlize模型,尝试使用以下代码片段。

StudentQuiz.findAll({
    attributes: [Sequelize.fn('max', Sequelize.col('score'))],
    group: ["student_id"],
    raw: true,
})