2015-11-19 44 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 
     }); 
    }); 

查詢方法最終將是更快然而