mysql查询时间周期,MySQL:周期日期范围从查询中的周号

I got a database table that looks something like this:

| id | clock | info

----------------------------------------------

| 1 | 1262556754 | some info

| 2 | 1262556230 | some other info

| 3 | 1262556988 | and another

| 4 | 1262555678 | and some more

It contains log records and a unix timestamp, when this log was written. What I need, is to get a weekly report, on how many log records there was during each week. Here is a query that I wrote:

SELECT

DATE_FORMAT(FROM_UNIXTIME(clock), "%U") AS week

count(*) as cnt

FROM logs

WHERE DATE_FORMAT(FROM_UNIXTIME(clock), "%Y") = '2010'

GROUP BY week

This gives a result like this:

| week | cnt

-------------------------------

| 1 | 55

| 2 | 134

| 4 | 765

| 20 | 65

Great! But what I would like to see, is a date ranges like 08 Feb 2010 00:00 - 15 Feb 2010 00:00, so my result set would look like this:

| day_start | day_end | cnt

---------------------------------------------------------

| 08 Feb 2010 00:00 | 15 Feb 2010 00:00 | 55

| 15 Feb 2010 00:00 | 22 Feb 2010 00:00 | 76

| 22 Feb 2010 00:00 | 01 Mar 2010 00:00 | 756

Is there any way to do this?

解决方案

Use STR_TO_DATE('201008 Monday', '%X%V %W'); to get a proper date like 2010-02-22 then use DATE_FORMAT to get the format you need.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值