如何以降序获取mysql数据? [英] How to fetch mysql data in descending order?

查看:97
本文介绍了如何以降序获取mysql数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的应用程序以json格式获取mysql数据并显示为 从ID1到ID * 我必须每天更新数据库表,我想首先显示最近的数据,我不想每次更新数据库时都更改整个表结构.

So my application gets mysql data in json format and displays it in id1 to id* I have to update my database table every day and I want to show the recent data first, I don't want to change the entire table structure each and every time I update my database.

有什么方法可以按升序添加行并按降序获取数据,以便我的应用程序首先显示新数据?

Is there any way that I can add rows in ascending order and fetch data in descending order so that my app will show the fresh data first?

这里是编码器:

$connection = mysqli_connect("domain", "database user", "password", "database name");
$id = $_GET["id"];
$query = "SELECT * FROM my_data WHERE id BETWEEN ($id+1) AND ($id + 10)";
$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($result)) {
    $array[] = $row;    
}

header('Content-Type:Application/json');
echo json_encode($array);

推荐答案

像这样使用order by desc

Select * from my_data where id between ($id+1) and ($id+10) order by id desc

此处数据按降序排列.并按照ID顺序订购.如果您想与其他任何字段一起订购.比您可以指定字段名称而不是ID.

Here your data shorted in descending order. and order follow in id. if you want to do order with any other field. than you can give name of field instead of id.

这篇关于如何以降序获取mysql数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆