2016-02-26 42 views
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'] 
}); 
+1

有沒有錯誤?查詢看起來像Sequelize爲您的示例產生的結果是什麼? – tiblu

回答

1

只要看一眼它,我會寫這樣

db.Workbook.findAll({ 
    attributes: ['wbquestion.question.topic.id', [db.sequelize.fn('COUNT', db.sequelize.col('wbquestion.question.topic.id'))], 'topiccount'], 
    include: [ 
    { 
     model: db.WorkbookQuestion, 
      as: wbquestion, 
     where: { 
       id: wbquestion.workbookId  
      }, 
      include: [ 
       model: db.Question, 
       as: question, 
       where: { 
        id: wbquestion.questionId 
       }, 
       include: [ 
        model: db.Topic, 
        as: topic, 
        where: { 
         id: question.topicId 
        } 
       ] 
      ] 
    } 
    ], 
    where: { 
     studentId : 3 
    } 
    group: ['topic.id'] 
}); 

代碼也許你可以得到更多的可讀通過分別定義模型和關係並使用之後的急切加載來實現代碼。

編輯: 生成的遷移和模型

node_modules/.bin/sequelize model:create --name Student --attributes firstName:string,lastName:string 
node_modules/.bin/sequelize model:create --name Topic --attributes topicText:string 
node_modules/.bin/sequelize model:create --name Question --attributes TopicId:integer 
node_modules/.bin/sequelize model:create --name Workbook --attributes studentId:integer 
node_modules/.bin/sequelize model:create --name WorkbookQuestion --attributes QuestionId:integer,WorkbookId:integer 

在控制檯日誌測試查詢的簡單程序。

var Sequelize = require("sequelize"); 

var models = require('./models/index'); 

models.WorkbookQuestion.belongsTo(models.Question, {foreignKey: 'QuestionId'}); 
models.WorkbookQuestion.belongsTo(models.Workbook, {foreignKey: 'WorkbookId'}); 

models.Question.hasOne(models.WorkbookQuestion); 

models.Workbook.hasOne(models.WorkbookQuestion); 
models.Workbook.belongsToMany(models.Question, {through: models.WorkbookQuestion}); 
models.Workbook.belongsToMany(models.Topic, {through: models.Question}); 


var res = models.Workbook.findAll({ 
    attributes: ['topicId', Sequelize.fn('count', Sequelize.col('topiccount'))], 
    include: [models.WorkbookQuestion, models.Question, models.Topic], 
    where: { 
     studentId: 3 
    }, 
    group: ['topic.id'] 
}).then(function(data) { 
    console.log(data); 
}).catch(function(err) { 
    console.trace(err); 
}); 

結果是:

SequelizeDatabaseError:列Topics.Question.WorkbookId不存在

也許你可以修復模型之間的關係,或者你不應該使用的findAll功能創建查詢。

相關問題