node.js - 如何在 Sequelize.js 中查询 Group by 和 MAX(id)

标签 node.js express group-by sequelize.js max

嗨,我想问一下如何按特定 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/

相关文章:

node.js - 如何直接在 Angular 4/expressJS 服务器中运行项目(无需 webpack)

node.js - 链接 Mongoose 查询的最佳方式

node.js - 嵌套 Jade 模板黑色

python - 如何使用类似分组的函数将值插入新列?

Mysql 在 CSV 字段中进行分组

node.js - 403/禁止使用 NodeJS/Express 在 favicon 上显示

javascript - 缓存并避免重新加载 html、css 和 js 文件

javascript - EJS的render()和renderFile()有什么区别?

javascript - 将变量传递给 node.js 中的 res.render?

AngularJS Group By 指令没有外部依赖