Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Sequelize.js中查询Group by和MAX(id)_Node.js_Express_Group By_Sequelize.js_Max - Fatal编程技术网

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

Node.js 如何在Sequelize.js中查询Group by和MAX(id),node.js,express,group-by,sequelize.js,max,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,

大家好,我想问一下如何通过特定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生成它?提前感谢

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

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

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

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