2015-11-19 41 views
1

我想比较Sails.js应用程序中我的Waterline查询中的两个字段,例如:SELECT * FROM entity E WHERE E.current < E.max比较Waterline/Sails.js中的两个字段查询

我尝试下面的代码,但它预计整数值要传递给它,而不是列名:

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

那么,如何比较两列?

+0

是否'Entity.find({电流:{ '<':最大}});'工作? – vincent

+0

什么应该是'max'变量的值? –

回答

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); 
    } 
}); 
+0

我明白了,谢谢。这是一个底层的SQL还是一些特殊的伪SQL? –

+0

@SlavaFominII这是模型方法的一部分,作为参考这个http://sailsjs.org/documentation/reference/waterline-orm/models/query – vincent

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 
     }); 
    }); 

查询方法最终将是更快然而