【问题标题】:PHP/MySQL: Sort by time, then by datePHP/MySQL:按时间排序,然后按日期
【发布时间】:2011-12-01 14:07:37
【问题描述】:

我偶然发现了一个我认为很容易解决的问题,但似乎让我发疯了。所以,我试图按时间对一些 MySQL 记录进行排序,然后按日期对它们进行“分组”。例如,这是我的 MySQL 数据:

+----+------------+----------+---------------------------+--------+
| id | date       | time     | entry                     | status |
+----+------------+----------+---------------------------+--------+
| 21 | 2011-10-05 | 09:42:06 | All systems online.       |      1 |
| 22 | 2011-10-05 | 09:43:09 | Maintenance starting.     |      2 |
| 23 | 2011-10-04 | 08:42:06 | Systems online and ready. |      1 |
| 24 | 2011-10-05 | 09:44:30 | Systems are offline.      |      0 |
+----+------------+----------+---------------------------+--------+

所以,我用来对所有内容进行排序的查询是:

SELECT * FROM status order by date ASC;

这会产生以下结果:

+----+------------+----------+---------------------------+--------+
| id | date       | time     | entry                     | status |
+----+------------+----------+---------------------------+--------+
| 21 | 2011-10-05 | 09:42:06 | All systems online.       |      1 |
| 22 | 2011-10-05 | 09:43:09 | Maintenance starting.     |      2 |
| 24 | 2011-10-05 | 09:44:30 | Systems are offline.      |      0 |
| 23 | 2011-10-04 | 08:42:06 | Systems online and ready. |      1 |
+----+------------+----------+---------------------------+--------+

PHP 输出是问题所在。所以,现在的输出是:

2011 年 10 月 4 日

  • 系统在线并准备就绪。 [上午 8 点 42 分]

2011 年 10 月 5 日

  • 所有系统在线。 [上午 9 点 42 分]

  • 维护开始。 [09:43 AM]

  • 系统处于脱机状态。 [上午 9 点 44 分]

我想要的输出是什么:

2011 年 10 月 5 日 - 系统离线。 [上午 9 点 44 分]

  • 维护开始。 [09:43 AM]

  • 所有系统在线。 [上午 9 点 42 分]

2011 年 10 月 4 日

  • 系统在线并准备就绪。 [上午 8 点 42 分]

基本上,我希望所有内容都按日期分组(最新的在前),并且我希望最近的时间在顶部,而不是底部。

这是我的 PHP 代码:

function getUpdates() {
    global $db;
    $updchk = "";
    $entries = $db->GetAll("SELECT * FROM status order by date DESC;");
    if (!$entries) { ?>
        <p>No entries in the database, yet.</p>
  <?php } else
    foreach ($entries as $entry) {
        if (ConvertDate($entry['date']) != $updchk) { ?>
            <h4><?php echo ConvertDate($entry['date']); ?></h4>
            <p><?php echo $entry['entry']; ?><span class="timestamp"> [<?php echo strftime('%I:%M %p', strtotime($entry['time'])); ?>]</span></p>
            <?php $updchk = ConvertDate($entry['date']); ?>
        <?php } else { ?>
            <p><?php echo $entry['entry']; ?><span class="timestamp"> [<?php echo strftime('%I:%M %p', strtotime($entry['time'])); ?>]</span></p>
        <?php }
    } ?>
<?php } ?>

感谢任何帮助。

谢谢!

【问题讨论】:

    标签: php mysql sql sorting


    【解决方案1】:

    只需在 ORDER BY 中添加一个额外的子句吗?

    SELECT ...
    FROM status
    ORDER BY `date` DESC, `time` DESC
    

    您可以根据需要对任意多个(或少数)字段进行排序,甚至可以根据需要对任意表达式进行排序。

    【讨论】:

      【解决方案2】:

      将您的查询更改为

      SELECT * 
      FROM status 
      order by `date` desc, `time` desc;
      

      【讨论】:

        【解决方案3】:

        在 SQL 中按日期和时间排序,

        SELECT * FROM status ORDER BY date DESC, time DESC;
        

        【讨论】:

          【解决方案4】:

          试试这个:

          SELECT * FROM `status` ORDER BY `date`, `time` DESC 
          

          【讨论】:

            【解决方案5】:
            SELECT * FROM `status` ORDER BY `date` DESC, `time` DESC
            

            适用于您的代码

            【讨论】:

            • 请告诉我们,您的答案与已接受的正确答案有什么区别,一年前发布!!!
            【解决方案6】:

            SELECT * FROM status order by DATE(date) asc,time desc;

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-10-28
              • 2012-12-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-09-30
              • 1970-01-01
              相关资源
              最近更新 更多