Truy vấn SQL thô (SUM) trong Loopback.JS

Trong lúc xài Loopback.JS, mình muốn dùng các từ khóa đặc biệt hơn như GROUP BY, SUM. Kết quả sau khi tham khảo 2 link:

https://loopback.io/doc/en/lb3/Executing-native-SQL.html 
https://stackoverflow.com/questions/35456579/loopback-group-by-ability-with-mysql 

Bên dưới là đoạn code mẫu trong app của mình để tính sum của cột "amount" trong bảng Transaction.
Đoạn mã được gọi trong một controller (remote method) của Loopback.


const Transaction = Stats.app.models.Transaction;
 

Transaction.dataSource.connector.query(util.format(
"SELECT SUM(amount) as totalAmount from Transaction where type = %d and status = %d and playerId = '%s'", 1, 4, playerId), function (err, results) {

 cb(err, results);
});

Comments

Popular Posts

Liên hệ

Name

Email *

Message *