如何按月B;年分组?

发布于 2024-09-15 23:53:17 字数 480 浏览 5 评论 0原文

http://www.bioshapebenefits.nl/bioshapebenefits/en/news/donations。正如

你所看到的,这里我有一个很长的条目列表,按日期排序,非常简单。例如,在 mysql 中只会有“name”和“date”列。如果我想用 php 制作一个列表,那也很简单,只需按日期排序即可。

但是,如示例所示,我该如何将月份+年份放在中间呢?喜欢:

2010 年 6 月

  • Trala
  • Lala
  • Wala

2010 年 5 月

  • Trala
  • Lala
  • Wala

http://www.bioshapebenefits.nl/bioshapebenefits/en/news/donations.html

As you see, here I have a long list of entries, sorted by date, very simple. So in mysql there would only be the columns 'name' and 'date', for example. And if I'd want to make a list out of it with php that would also be very simple, just sorting by date.

But how would I put the months+years in between, as shown in the example? Like:

June 2010

  • Trala
  • Lala
  • Wala

May 2010

  • Trala
  • Lala
  • Wala

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┼── 2024-09-22 23:53:17

在 SQL Server 中,它会像下面一样简单,我想 mysql 也有类似的东西:

GROUP BY Year(Date), Month(Date)

In SQL Server it would be as simple as the following, I'd imagine mysql has something similar:

GROUP BY Year(Date), Month(Date)

始终不够 2024-09-22 23:53:17

有十几种方法可以给这只猫剥皮,但我的方法可能是这样的(所有粗略的代码片段,不是完整的实现)

首先,查询

select `name`
     , month(`date`) as date_month
     , year(`date`) as date_year
  from [Table]
 order by `date` desc

然后,将数据组织到所需的逻辑组中

$templateData = array();
foreach ( $rows as $row )
{
  $templateData[$row->date_year][$row->date_month][] = $row->name;
}

然后,在模板中

<?php foreach ( $templateData as $year => $months ) : ?>
  <?php foreach ( $months as $month => $names ) : ?>
    <h2><?php echo $month, ' ', $year; ?></h2>
    <ul>
      <?php foreach ( $names as $name ) : ?>
        <li><?php echo $name; ?></li>
      <?php endforeach; ?>
    </ul>
  <?php endforeach; ?>
<?php endforeach; ?>

There's a dozen ways to skin this cat, but my approach would probably be like this (all rough code snippets, not a full implementation)

First, the query

select `name`
     , month(`date`) as date_month
     , year(`date`) as date_year
  from [Table]
 order by `date` desc

Then, organize the data into the desired logical groups

$templateData = array();
foreach ( $rows as $row )
{
  $templateData[$row->date_year][$row->date_month][] = $row->name;
}

Then, in a template

<?php foreach ( $templateData as $year => $months ) : ?>
  <?php foreach ( $months as $month => $names ) : ?>
    <h2><?php echo $month, ' ', $year; ?></h2>
    <ul>
      <?php foreach ( $names as $name ) : ?>
        <li><?php echo $name; ?></li>
      <?php endforeach; ?>
    </ul>
  <?php endforeach; ?>
<?php endforeach; ?>
孤君无依 2024-09-22 23:53:17

我们对本月的吨位进行分组。我使用此表达式将任意时间戳截断为代表该月第一天的时间戳。

   TIMESTAMP(DATE_FORMAT(action_time,'%Y-%m-01'))

这与 Oracle-ism TRUNC(action_time,'MM') 的作用相同,

例如您可以这样做,

SELECT COUNT(*) NUM, TIMESTAMP(DATE_FORMAT(t.action_time,'%Y-%m-01')) MONTH
FROM TABLE t
GROUP BY TIMESTAMP(DATE_FORMAT(t.action_time,'%Y-%m-01'))

这具有概念上的优势,它维护了月份截断数据的时间戳,因此如果您如果需要,您仍然可以计算时差并做其他及时和过时的事情。

正如 raven 指出的那样,这会带来一点性能损失(但这还不错!)。我们的一些表列是使用此函数加载的,因此它们已经按月截断了。

We do tonnage of this month grouping. I use this expression to truncate arbitrary timestamps to timestamps that represent the first of the month.

   TIMESTAMP(DATE_FORMAT(action_time,'%Y-%m-01'))

This does the same thing as the Oracle-ism TRUNC(action_time,'MM')

For example you could do

SELECT COUNT(*) NUM, TIMESTAMP(DATE_FORMAT(t.action_time,'%Y-%m-01')) MONTH
FROM TABLE t
GROUP BY TIMESTAMP(DATE_FORMAT(t.action_time,'%Y-%m-01'))

This has the conceptual advantage that it maintains the timestampiness of the month-truncated data, so if you need to you can still compute time differences and do other timey and datey things if you need to.

As ravern pointed out, there's a small performance penalty (but it isn't bad!). Some of our table columns are loaded using this function so they're already month-truncated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文