简体   繁体   中英

How can I get distinct column values using LoopBack REST API

I have loopback REST API, which returns all the data from MySQL table in JSON format. But, I want to query or apply filter where it returns only distinct values for a given column. For eg in SQL we use this syntax :SELECT DISTINCT column1 FROM table_name; How can I achieve this using LoopBack Rest API? Does LoopBack support something like above? Please suggest me any other solution.

To my knowledge there is no filter for DISTINCT.

You can execute the sql command instead and receive the JSON object in return

var sql = "select DISTINCT name from user";
var ds = {YourModel}.app.datasources.{yourConnectionName};
ds.connector.execute(sql, [], function(err, data) {
// your code here
});

Refer http://loopback.io/doc/en/lb2/Executing-native-SQL.html for more details.

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