简体   繁体   中英

Sails.js/waterline: groupBy + count

Using sails.js (0.10.5):

I can't figure out how to achieve a groupBy/count using waterline/sails. What I would like to have is something like:

SELECT customer_id, COUNT('') FROM customer_data GROUP BY customer_id;
or
SELECT customer_id, SUM(1) FROM customer_data GROUP BY customer_id;

I wasn't able to achieve this using count nor sum (using a constant):

Customer.find({ groupBy:['customer_id'], count: true })
or
Customer.find({ groupBy:['customer_id'], sum: [1] })

Both will end in an error, the first "missing calculation" and the second "SQL syntax error". I can make a grouping using sum with an existing column ( sum: ['id'] ) but that does not get me the count.

Am I using the API wrong?

please use Customer.query('SELECT customer_id, SUM(1) FROM customer_data GROUP BY customer_id', function(err,results){})

if you don't want use sql, you can like this to use Customer.find({groupBy:'customer_id'}).exec

ref: waterline-sequel select builder function

.query() is deprecated as of Sails v1.0, instead use .getDatastore().sendNativeQuery() (ref: https://sailsjs.com/documentation/reference/waterline-orm/models/query ). There's still no way to do this with Waterline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM