Compare two fields in Waterline/Sails.js query

安稳与你 提交于 2020-02-06 05:46:06

问题


I want to compare two fields in my Waterline query in Sails.js application, e.g.: SELECT * FROM entity E WHERE E.current < E.max.

I've tried the following code, but it's expecting integer value to be passed to it instead of column name:

Entity.find({
  where: {
    current: {'<': 'max'}
  }
});

So, how do I compare two columns?


回答1:


I have ran some tests and at the same time read the Waterline documentation. There is no indication of anything that could possibly do comparison of two fields/columns via .find() or .where() methods. Reference: http://sailsjs.org/documentation/concepts/models-and-orm/query-language

Instead, I have used .query() method to compare two fields via SQL string such as :

Entity.query("SELECT * FROM `Entity` E WHERE E.current < E.max", function( err, res) {
  if(err) {
    //exception
  } else {
    console.log('response',res);
  }
});



回答2:


The other way would be to use one query to get the max before putting it in the criteria.

EntityOne.find({
  sort: 'column DESC'
}).limit(1)
  .exec(function(err,found){
    var max = found[0]
    EntityTwo.find({
      where: {
        current: {'<': found}
      }
    }).exec((err,found) {
      // Do stuff here
      });
  });

The query method is ultimately going to be faster however



来源:https://stackoverflow.com/questions/33815146/compare-two-fields-in-waterline-sails-js-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!