【问题标题】:sum hits for every week in mysqlmysql中每周的总和
【发布时间】:2012-05-06 16:45:04
【问题描述】:

我有一张表,其中包含每天的页面浏览量。像这样的:

+------+------------+------+----------+
|  id  | date       | hits | mangaID  |
+------+------------+------+----------+
| 4876 | 1331843400 | 132  |    13    |
+------+------------+------+----------+
| 4876 | 1331929800 | 24   |    236   |
+------+------------+------+----------+
| 7653 | 1331929800 | 324  |    13    |
+------+------------+------+----------+

我正在尝试使用以下代码从上周获得总命中数:

SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'

我的问题是我在日期字段中使用 strtotime 作为 int 类型将日期存储为时间。

那我怎样才能得到我想要的!?

【问题讨论】:

    标签: php mysql database datetime


    【解决方案1】:

    试试这个:

    select sum(hits) hitCount from t
    where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
    

    这是fiddle 玩。

    我稍微更改了您的数据,因为您提供的记录超过 7 天,因此 sum 将返回 0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      相关资源
      最近更新 更多