如何在续集中select个案查询?

我有一个SQL查询:

SELECT field1, field2, CASE WHEN field1=1 THEN 'a' ELSE 'b' END AS field3 FROM test 

我想用sequelizejs来实现它,

 const params = { attributes: //DO SELECT CASE, }; yield Model.findAll(params); 

谁能帮我? 谢谢。

       

网上收集的解决方案 "如何在续集中select个案查询?"

对于那些仍在寻找这个答案的人来说

 Model.findAll({ attributes: [[models.sequelize.literal('CASE WHEN "field1" = true THEN 55 ELSE 23 END'), 'field3']] } 

要么

 Model.findAll({ attributes: { include: [[models.sequelize.literal('CASE WHEN "field1" = true THEN 55 ELSE 23 END'), 'field3']]} } 

所以在我的示例中,当field1为true时,它将返回55 else 23.这将生成一个类似的查询

 SELECT CASE WHEN "field1" THEN 55 ELSE 23 END AS "field3" FROM "models" 

有关更多信息,请参阅文档http://docs.sequelizejs.com/en/latest/docs/querying/#attributes