【问题标题】:Where query with "internal relation" of the searched objectWhere 查询与搜索对象的“内部关系”
【发布时间】:2014-11-03 16:30:06
【问题描述】:

我想使用 sequelize 来生成简单的查询,类似于:

SELECT * FROM `Products`
WHERE `Product`.`oldPrice` > `Product`.`newPrice`

目前我会做一些丑陋的原始查询,例如:

database.Product.find({ where: '`Product`.`oldPrice` > `Product`.`newPrice`' })

这并不是什么不好的做法,但我想知道我是否没有错过文档中的一段,它将向我介绍一种更简洁的形式,例如 mongo 函数表达式(请参阅:http://mongoosejs.com/docs/api.html#query_Query-%24where)。

database.Product.find({ where: function () {
    return (this.oldPrice > this.newPrice);
});

是否有这样的捷径,或者在这种情况下我必须继续编写 SQL 吗?

提前致谢。

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:
    Product.find({
        where: {
            oldPrice: {
                gt: sequelize.col('newPrice')
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 2021-09-05
      • 2019-01-10
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多