我如何在Waterline中执行COUNT和GROUP BY?
我想在Waterline中执行这个查询:
SELECT priority, count(*) AS Num FROM Ticket GROUP BY priority
我不知道“countByName”函数如何工作,我没有find一个合适的例子或解释。
我也试着打电话给它
Model.query('SELECT ...')
但是这只是返回undefined。
我认为Model.query不会返回任何东西,应该给它一个callback。 它应该看起来更像是:
Model.query("SELECT priority, COUNT(*) as num FROM ticket GROUP BY priority", function(error, counts) { if(error) console.log(error); // Do something with the results here. console.log(counts); });
编辑:在一些研究,你不能使用计数,但你可以在Sails中使用一个组的其他计算,但语法似乎没有很好的logging:
Something.find({ groupBy: [ 'keyfield' ], sum: [ 'totalAmt' ] }) .done(function(error, response) { console.log(response); });