如何使用Sequelize查询两列是否相等?

时间:2016-02-27 03:17:59

标签: postgresql sequelize.js

url(about:blank)

查询生成此SQL:db.WorkbookQuestion.count({ where: { QuestionId: dbQuestion.id, AnswerSelectedId: db.sequelize.col('CorrectAnswerId') } });

如何让它更像SELECT count(*) AS "count" FROM "WorkbookQuestions" AS "WorkbookQuestion" WHERE "WorkbookQuestion"."QuestionId" = 1103 AND "CorrectAnswerId";

3 个答案:

答案 0 :(得分:9)

您可以通过sequelize.where()

来完成
db.WorkbookQuestion.count({
  where: sequelize.where(
    db.sequelize.col('CorrectAnswerId'),
    db.sequelize.col('AnswerSelectedId')
  )
});

答案 1 :(得分:6)

今天我被困在这一段时间,直到找到这个答案。我还找到了另一种做同样查询的方法,所以在这里会留下另一个答案。

db.WorkbookQuestion.count({
  where: {
    CorrectAnswerId: {
        $col: 'AnswerSelectedId'
    }
  }
});

答案 2 :(得分:0)

更新

const { Op } = require('sequelize');

db.WorkbookQuestion.count({
  where: {
    QuestionId: dbQuestion.id,
    CorrectAnswerId: {
        [Op.eq]: sequelize.col('AnswerSelectedId')
    }
  }
});