将我的数据库中的前 10 名玩家打印到页面的最简单方法是什么?

What would be the easiest way to print the top 10 players from my database to a page?

我正在使用 mysql 和 sails.js。当我想查询我用的游戏前10名时:

Select name, totalwins/totalgames from user order by totalwins desc limit 10;

这在我的终端中显示如下内容(例如缩短为 3):

+-----------------------+----------------------+
| name                  | totalwins/totalgames |
+-----------------------+----------------------+
| name 1                |               1.7917 |
| name 2                |               1.5417 |
| name 3                |               0.3333 |
+-----------------------+----------------------+

如果我还想将其打印到我的页面上以创建一个在刷新时更新的记分牌,我该怎么做呢? mysql 有通用的方法吗?

也许我可以假设您有一个帆模型和控制器:UserUserController。 (如果没有,sails 文档或 in-project 评论应该使这些易于创建)。然后,您可以通过将此添加到路由文件 (/config/routes.js):

来设置要从 UserController 中的方法提供的页面
'GET /mypage': 'UserController.displayPage',

然后在你的 UserController (/api/controllers/UserController.js):

module.exports = {
    displayPage: function(req, res) {
        User.find({}).sort('totalWins DESC').limit(10).exec(function(err, users) {
            if (err) { /* handle errors */ }
            // users has the objects you want in the order you specified
            // send them to a view using res.view
            return res.view('myview', {users: users});
        });
    },
    // ...
}

这会将数据发送到位于 /views/myview.ejs 的视图。这是一个可模板化的(基本上)html 页面,但在其中您可以访问您的 users object 并使用标准模板符号。例如:

<% for (var idx = 0; idx < users.length; idx++) { %>
<tr>
    <td><%= users[idx].name %></td>
</tr>
<% } %>

等等

步骤很多,但是none难以逾越!