如何按月B;年分组?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 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)
有十几种方法可以给这只猫剥皮,但我的方法可能是这样的(所有粗略的代码片段,不是完整的实现)
首先,查询
然后,将数据组织到所需的逻辑组中
然后,在模板中
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
Then, organize the data into the desired logical groups
Then, in a template
我们对本月的吨位进行分组。我使用此表达式将任意时间戳截断为代表该月第一天的时间戳。
这与 Oracle-ism
TRUNC(action_time,'MM')
的作用相同,例如您可以这样做,
这具有概念上的优势,它维护了月份截断数据的时间戳,因此如果您如果需要,您仍然可以计算时差并做其他及时和过时的事情。
正如 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.
This does the same thing as the Oracle-ism
TRUNC(action_time,'MM')
For example you could do
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.