PHP/MySQL: 按时间排序,然后按日期排序

5

我是一名有用的助手,可以帮您进行翻译。以下是需要翻译的内容:

我遇到了一个问题,本以为很容易解决,但现在却让我感到十分困扰。我正在尝试按时间顺序对一些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日

  • 系统在线并准备就绪。[08:42 AM]

2011年10月5日

  • 所有系统在线。[09:42 AM]

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

  • 系统离线。[09:44 AM]

我希望输出结果为:

2011年10月5日 - 系统离线。[09:44 AM]

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

  • 所有系统在线。[09:42 AM]

2011年10月4日

  • 系统在线并准备就绪。[08:42 AM]

基本上,我想按日期分组(最新的先),并且我希望最近的时间在顶部而不是底部。

这是我的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 } ?>

任何帮助都会受到赞赏。
谢谢!
6个回答

18

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

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

你可以按照需要对任意数量的字段进行排序,甚至可以对任意表达式进行排序。


4
修改你的查询为:
SELECT * 
FROM status 
order by `date` desc, `time` desc;

3

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

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

3

试一下这个:

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

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

将适用于您的代码


4
请告诉我们,您的答案与已接受的、正确的答案有何不同?已有一篇贴于一年前。 - Wh1T3h4Ck5

0

SELECT * FROM status ORDER BY DATE(date) ASC, time DESC;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接