如何按降序获取mysql数据?

时间:2016-10-21 11:17:11

标签: php mysql

所以我的应用程序以json格式获取mysql数据并显示它 id1到id * 我必须每天更新我的数据库表,并且我想先显示最近的数据,我不想在每次更新数据库时更改整个表结构。

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

这是编码器:

$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);

4 个答案:

答案 0 :(得分:7)

像这样使用order by desc

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

此处您的数据按降序排列。并按顺序进入id。如果你想与任何其他领域订购。你可以给出字段名而不是id。

答案 1 :(得分:1)

你的代码应该是这样的

<?php 

$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) order by id desc";
    $result = mysqli_query($connection,$query);
    while ($row = mysqli_fetch_assoc($result)) {

        $array[] = $row;    
    }
    header('Content-Type:Application/json');
    echo json_encode($array);

 ?>

希望这会对你有所帮助!!

答案 2 :(得分:0)

参考:

SQL Order By

答案 3 :(得分:0)

使用以下语法

SELECT * FROM Table_Name order by Column_name DESC