简体   繁体   English

Node.js在MySQL上获取数据并对最新数据进行排序

[英]Node.js Get data on MySQL and sorting newest data

I'm trying to get the data from MySQL using EJS. 我正在尝试使用EJS从MySQL获取数据。 What I need is, to get the newest users to start at the top of the table but I don't know how to do it. 我需要的是让最新的用户从表格的顶部开始,但我不知道该怎么做。 All I can do is just get oldest to newest 我所能做的就是变老到最新

<% for(var i=0; i<users.length; i++ ){ %>
            <tr>
                <td> <%= users[i].username %> </td>
                <td> <%= users[i].first_name %> </td>
                <td> <%= users[i].last_name %> </td>
                <td> <%= users[i].email %> </td>
                <td> <%= users[i].type %> </td>
            </tr>
            <% } %>

You can achieve here in sql query. 您可以在这里通过sql查询来实现。

  1. You need to have a field 'updated' in your user table. 您需要在用户表中将字段“更新”。

  2. sorted by 'updated' field 按“更新”字段排序

    SELECT username, first_name, last_name, email, type, updated FROM users ORDER BY updated DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM