Hej
我的SQL技能非常有限,所以我需要一些帮助。
我有一个人们可以在 Azure 移动服务表中投票的元素表。看起来像这样:
表名
ID | Content | Votes |
从这个表格中,我想得到得票最高的20个元素。如果有些人有相同的选票,它应该只选择其中之一。最佳方法是,我使用 GET 帖子到 MobileService API 可以选择最高投票范围,这样的新调用将获得接下来的 20 个元素。
Azure 中的 API 如下所示:
exports.get = function(request, response) {
//some code
response.send();
}
并且可以调用
var results = await App.MobileService.InvokeApiAsync<Class>("APIname", System.Net.Http.HttpMethod.PostorGet, DictionaryOfInformationSendToTheAPI);
所以我想要的是我发送一个 HttpMethod.Get 命令,我将字典中想要的范围发送到 API 调用。然后,API 调用将通过返回 ID 或整行来响应从表 tableName 中获取信息。
我不知道怎么回事,这里只是猜测获得最大的命令,但希望有人可以帮助我创建这个命令,类似
exports.get = function(request, response) {
//Not working so just a guess
var sqlSpecs = "READ from tableName WHERE votes is" + request.rangeMax + " to " + request.rangemin;
mssql.query(sqlSpecs, {
success: function(items)
{
console.log("ok");
response.send(items);
},
error: function(err)
{
console.log("there was a problem finding the Specified range" + request.rangeMax + " to " + request.rangemin, "" + err);
}
});
}
最后,我当然对如何更智能地访问它的其他想法持开放态度。也许是前 20 名的额外表格?但对我来说,问题是基础设施会大得多,因此认为SQL命令必须更智能。但请用想法加入。
exports.get = function(request, response) {
var collectionOfVotes = request.service.tables.getTable('CollctionOfVotes');
collectionOfVotes.orderByDescending('Votes')
.read({ success: function(results) {
results.forEach(function(r) {
console.log(r);
});
}});
};
or
exports.get = function(request, response)
{
mssql.query('select top 20 * from CollctionOfVotes order by Votes desc',
{
success: function(results)
{
results.forEach(function(r) {
console.log(r);
});
},
error: function(err)
{
console.log("error is: " + err);
}
});
}
您可以像这样访问您的表:
exports.get = function(request, response) {
var myTable = request.service.tables.getTable('tableName');
// Do something with the table here…
};
或者,您可以完全按照您正在执行的方式执行查询:
exports.get = function(request, response)
{
mssql.query('select top 20 * from tablename',
{
success: function(results)
{
console.log(results);
},
error: function(err)
{
console.log("error is: " + err);
}
});
}