SQL查询输出的反向顺序 [英] Reverse order of sql query output

查看:532
本文介绍了SQL查询输出的反向顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我无法解决的问题.我正在使用PHP通过数据库运行以下内容:

I have a bit of a problem that I cannot solve. I am running the following through my DB using PHP:

$strQuery = "select * from LastResult ORDER BY Date DESC LIMIT 10";

所有结果均恢复正常,并达到预期效果,但是随后我必须将其输入折线图中,但是当我这样做时,它们显然会反向显示,因为我有数据库按日期将它们返回到DESC中,这意味着最近的将是第一个返回的.

The results all come back fine and as expected, however I then have to feed these into a line chart but when I do so, they are obviously displayed in reverse as I have the DB to return them in DESC by Date which means the most recent will be the first returned.

有没有一种方法可以在返回这些结果之后在将数据输入到图表之前反转它们的顺序.

Is there a way that after returning these results I can reverse their order before feeding the data to my chart.

这里是完整的查询(请不要评论使用mysql而不是mysqli的用法,我没写那个位)

Here is the full query (please don't comment about the use of mysql instead of mysqli, I didn't write that bit)

$strQuery = "select * from LastResult ORDER BY Date DESC LIMIT 10";

$result3 = mysql_query($strQuery) or die(mysql_error());

if ($result3) {
    while($ors4 = mysql_fetch_array($result3)) {
        $NumberResults2 = $ors4['Date'];

        $strQuery = "select AvGoalDifference as Average from LastResult where Date= '$NumberResults2'";

        $result4 = mysql_query($strQuery) or die(mysql_error()); 
        $ors3 = mysql_fetch_array($result4); 

        $strXML .= "<set label='" . $NumberResults2 . "' value='" . $ors3['Average'] . "' />";

        mysql_free_result($result4);
    }
}

mysql_close($link);

$strXML .= "</chart>";
$chart2 = renderChart("charts/Line.swf", "", $strXML, "AverageGD", 500, 260, false, true, true);

echo $chart2;

推荐答案

您可以通过执行外部选择并按所需方式(ASC)对结果集进行重新排序:

You can re order that resultset by doing a outer select and ordering it the way you want (ASC):

SELECT * FROM (
    select * from LastResult ORDER BY Date DESC LIMIT 10
) as
ORDER BY Date ASC;

编辑:如果失败(如在Postgresql 9.3.4中那样),请对内部 select 使用别名,如下所示:

Edit: If this fails (as it would in postgresql 9.3.4), use an alias for the inner select to make it as below:

SELECT * FROM (
    select * from LastResult ORDER BY Date DESC LIMIT 10
) as foo
ORDER BY foo.Date ASC;

这篇关于SQL查询输出的反向顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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