dongzanghua8422 2016-10-21 11:17
浏览 57
已采纳

如何按降序获取mysql数据?

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?

Here is the encoder:

$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);
  • 写回答

3条回答 默认 最新

  • dragoninasia2014 2016-10-21 11:18
    关注

    use order by desc like this

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站