我想在Sails.js应用程序中比较我的Waterline查询中的两个字段,例如:SELECT * FROM entity E WHERE E.current < E.max
。
我已经尝试了以下代码,但是它希望将整数值传递给它而不是列名:
Entity.find({
where: {
current: {'<': 'max'}
}
});
那么,如何比较两列?
答案 0 :(得分:3)
我已经进行了一些测试,同时阅读了Waterline文档。没有迹象表明可以通过.find()
或.where()
方法比较两个字段/列。参考:http://sailsjs.org/documentation/concepts/models-and-orm/query-language
相反,我使用.query()
方法通过SQL字符串比较两个字段,例如:
Entity.query("SELECT * FROM `Entity` E WHERE E.current < E.max", function( err, res) {
if(err) {
//exception
} else {
console.log('response',res);
}
});
答案 1 :(得分:0)
另一种方法是在将其放入标准之前使用一个查询来获取最大值。
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
});
});
查询方法最终会更快