简体   繁体   English

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

[英]PHP/MySQL: Sort by time, then by date

I've stumbled onto an issue I thought would be easy to resolve, but seems to be driving me crazy. 我偶然发现了一个我认为容易解决的问题,但似乎让我发疯了。 So, I'm trying to sort some MySQL records by time, and then sort of "group" them by date. 所以,我试图按时间排序一些MySQL记录,然后根据日期对它们进行“分组”。 For example, here's my MySQL data: 例如,这是我的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 |
+----+------------+----------+---------------------------+--------+

So, the query I use to get everything sorted is: 所以,我用来获取所有内容的查询是:

SELECT * FROM status order by date ASC;

Which yields the following results: 这产生以下结果:

+----+------------+----------+---------------------------+--------+
| 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 |
+----+------------+----------+---------------------------+--------+

The PHP output is the issue. PHP输出是个问题。 So, the output RIGHT NOW is: 所以,现在的输出是:

October 4, 2011 2011年10月4日

  • Systems online and ready. 系统在线并准备就绪。 [08:42 AM] [08:42 AM]

October 5, 2011 2011年10月5日

  • All systems online. 所有系统在线。 [09:42 AM] [09:42 AM]

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

  • Systems are offline. 系统离线。 [09:44 AM] [09:44 AM]

What I WANT the output to be: 我希望输出是什么:

October 5, 2011 - Systems are offline. 201110月5日 - 系统离线。 [09:44 AM] [09:44 AM]

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

  • All systems online. 所有系统在线。 [09:42 AM] [09:42 AM]

October 4, 2011 2011年10月4日

  • Systems online and ready. 系统在线并准备就绪。 [08:42 AM] [08:42 AM]

Basically, I'm wanting everything grouped by date (latest first) and I want the most recent time at the top, not the bottom. 基本上,我想要按日期分组的所有内容(最新的第一个),我希望最近的时间位于顶部,而不是底部。

Here is my PHP code: 这是我的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 } ?>

Any help is appreciated. 任何帮助表示赞赏。

Thanks! 谢谢!

just add an extra clause to the ORDER BY? 只需在ORDER BY中添加一个额外的子句?

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

You can sort on as many (or few) fields as you want, even on arbitrary expressions if need be. 您可以根据需要对任意数量(或很少)的字段进行排序,甚至可以根据需要对任意表达式进行排序。

change your query to 将您的查询更改为

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

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

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

试试这个:

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

将适用于您的代码

SELECT * FROM状态顺序按日期(日期)asc,time desc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM