Maison  >  Questions et réponses  >  le corps du texte

mysql - 如何按照时间段group by

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

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

以下是一个季度一个跨度

黄舟黄舟2743 Il y a quelques jours736

répondre à tous(2)je répondrai

  • 天蓬老师

    天蓬老师2017-04-17 13:35:22

    把时间映射成数字, 用计算列, 然后group by

    répondre
    0
  • 高洛峰

    高洛峰2017-04-17 13:35:22

    季度就用月份运算一个新的值,然后group by 这个字段
    (month-1)/4+1
    1 2 3 4 ---> 1

    5 6 7 8 ---> 2

    然后,7天就用星期值,
    mysql 有函数取某个日期 regdate 的月份或者星期数。

    比如mongodb 就有这样的

    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 的文档
    mysql 相关的日期函数

    répondre
    0
  • Annulerrépondre