【问题标题】:Distinct query on loopback-connector-mysqlloopback-connector-mysql 上的不同查询
【发布时间】:2015-11-21 16:34:59
【问题描述】:

我正在使用环回,我想从 Job 模型中获取所有唯一的位置名称。我试过了

Job.find({
    where: {
        location:distinct
    }
});

但它不起作用。

【问题讨论】:

    标签: loopbackjs strongloop


    【解决方案1】:

    目前 Loopback 中没有 distinct 关键字。但我相信存在 DISTINCT 关键字来查询 MySQL 中的不同列。所以你可以使用这个方法来执行原生 sql 查询。检查文档here。下面是如何使用它的示例代码。

    module.exports = function(Job) {
        Job.distinctLocations = function(byId, cb){
            var ds = Job.dataSource;
            var sql = "SELECT DISTINCT location FROM Job";  //here you write your sql query.
            ds.connector.execute(sql, byId, function(err, jobs) {
                if (err) console.error(err);
                cb(err, jobs);
            });
        };
        Job.remoteMethod(
            'distinctLocations',
            {
                http: {verb: 'get'},
                description: "Get distinct locations for the jobs.",
                returns: {arg: 'locations', type: 'object', root: true}
            }
        );
    };
    

    【讨论】:

    • 它在没有 arg: 'locations' in return 的情况下对我有用
    • 我使用arg: 'locations' 获得格式为:{'locations': {result object}} 的结果。我认为您的结果看起来像{result object}
    • 提供arg 提供result 值的键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    相关资源
    最近更新 更多