【问题标题】:Most Recent Entries in PHP/MySQL Ordered by AscendingPHP/MySQL 中的最新条目按升序排列
【发布时间】:2017-07-08 18:24:12
【问题描述】:

我想显示七个最近的条目,但我希望使用 PHP/MySQL 以升序对条目进行排序。这是我当前的代码:

$sql="SELECT date, weight, COUNT(*) AS 'count' FROM weights GROUP BY date ORDER BY date ASC";

注意:我已尝试添加“LIMIT 7”,但这只会显示条目 1-7(而不是 15-21,这正是我想要的)。

另请注意:我曾尝试在 Stack Overflow 和 Google 上进行搜索,但代码不起作用。

【问题讨论】:

  • LIMIT 15, 7 会告诉你 15 - 21
  • 您可以在此处阅读更多信息 dev.mysql.com/doc/refman/5.7/en/select.htmlWith two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.
  • 我要的是最近的七个条目,而不是像 15-21 这样的特定范围。

标签: php mysql sorting


【解决方案1】:

多亏了其他一些 Stack Overflow 线程,我才得以正常工作。

注意:我的代码略有不同。

这是我的代码:

$sql="SELECT id, date, weight FROM (SELECT id, date, weight, COUNT(*) AS 'count' FROM weights GROUP BY date ORDER BY id DESC LIMIT 7) t ORDER BY id ASC";

参考文献:

MYSQL select last 3 rows, order by ASC

Select last 20 order by ascending - PHP/MySQL


注意:我不知道...之间的区别...

t ORDER BY id ASC

还有……

tmp order by tmp.id asc

我选择了第一个,因为它恰好是我开始工作的第一个(这需要一段时间),而且看起来更简单。

如果有人知道 GROUP BY 和 ORDER BY 值是否正确,请发表评论。

还请发表评论,让我知道我应该使用哪个版本的 ASC。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 2016-10-05
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多