mysql - 如何按照时间段group by
黄舟
黄舟 2017-04-17 13:33:22
0
2
741

mysql如何分别按照7天,一个季度 来group by 这个时间阶段的count
时间字段存的是regdate

参考百度统计
以下是七天一个跨度

以下是一个季度一个跨度

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
Peter_Zhu

Map time into numbers, use calculated columns, and then group by

小葫芦

For the quarter, use the month to calculate a new value, and then group by this field
(month-1)/4+1
1 2 3 4 ---> 1

5 6 7 8 ---> 2

Then, use the week value for 7 days.
Mysql has a function to get the month or week number of a certain date regdate.

For example, mongodb has this

db.sales.aggregate(
   [
     {
       $project:
         {
           year: { $year: "$date" },
           month: { $month: "$date" },
           day: { $dayOfMonth: "$date" },
           hour: { $hour: "$date" },
           minutes: { $minute: "$date" },
           seconds: { $second: "$date" },
           milliseconds: { $millisecond: "$date" },
           dayOfYear: { $dayOfYear: "$date" },
           dayOfWeek: { $dayOfWeek: "$date" },
           week: { $week: "$date" }
         }
     }
   ]
)

mongodb’s documentation
mysql related date functions

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!