5

我想在 Waterline 中执行此查询:

SELECT priority, count(*) AS Num FROM Ticket GROUP BY priority

我不知道“countByName”函数是如何工作的,也找不到合适的示例或解释。

我也试着用

Model.query('SELECT ...')

但这只是返回未定义。

4

1 回答 1

6

I think that Model.query isn't going to return anything, it should be given a callback. It should look more like:

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

Edit: Upon some research you can't use count, but you can use other calculations with a group by in Sails but the syntax doesn't appear to be well documented.:

Something.find({ groupBy: [ 'keyfield' ], sum: [ 'totalAmt' ] })
  .done(function(error, response) {
    console.log(response);
  });
于 2014-01-02T17:40:52.570 回答