1
我的SQL查詢如何將複雜的GROUP BY轉換爲Sequelize?
SELECT t."id", t."topicText", COUNT(t."id") AS topicCount
FROM "WorkbookQuestions" wq, "Workbooks" w, "Questions" q, "Topics" t
WHERE w."StudentId" = 3
AND wq."QuestionId" = q."id"
AND wq."WorkbookId" = w."id"
AND q."TopicId" = t."id"
GROUP BY "t"."id"
ORDER BY "topiccount" DESC
我怎樣才能Sequelize
代碼寫這篇文章,所以我可以直接避免編寫SQL?還是不可能?
這是我嘗試過什麼(失敗)至今:
db.WorkbookQuestion.findAll({
attributes: ['Topics.id', [db.sequelize.fn('COUNT', db.sequelize.col('Topics.id'))], 'TopicCount'],
include: [
{
model: db.Workbook,
where: {
StudentId: newWorkbook.StudentId,
SubjectId: newWorkbook.SubjectId
}
}, {
model: db.Question,
include: [
{
model: db.Topic
}
]
}
],
group: ['Topics.id']
});
有沒有錯誤?查詢看起來像Sequelize爲您的示例產生的結果是什麼? – tiblu