有没有办法按过滤器执行分组?
类似于以下内容:
model.find({groupBy: 'myField'})
我正在使用 PostgreSQL。
最佳答案
基于 this page您可以执行 native SQL 查询,但是,如果它仍然像他们所说的那样工作,我们没有关于此的更新:
Warning: This feature has not been fully tested and is not officially supported: the API may change in future releases. In general, it is always better to perform database actions through connected models. Directly executing SQL may lead to unexpected results, corrupted data, and other issues.
对于数据源 > 1.x
dataSource.connector.execute(sql_stmt, params, callback);
或 =< 1.x
dataSource.connector.query(sql_stmt, params, callback);
1.sql_stmt - SQL 字符串。
2.params - SQL 语句的参数。
如果您有一个参数化查询,例如:
SELECT * FROM TABLE WHERE id = $1 or SOMETHING = $2
参数将是:
params = [358, "SOMETHING"];
3.callback - 回调函数
4.示例
module.exports = function (Collection) {
var ds = Collection.dataSource;
var sql = "SELECT _id FROM table GROUP BY my_field;";
var params = []; // = null (could be null also)
ds.connector.execute(sql, params, function (err, data) {
if (err) console.error(err);
console.log(data);
});
};
如果
query
或 execute
不工作尝试 executeSQL
关于loopbackjs - 环回查询 - 如何分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39075918/