获取特定时间范围内的数据库记录

时间:2016-04-26 10:18:20

标签: php mysql sql

我们假设我有一个名为video_data的下表。我有另一个videos,但这并不是真正相关的。我用它来查询特定频道的视频,但这不会改变查询。这只是为了获得一个通道的计算而不是全部。

+----+----------+-------+---------------------+
| id | video_id | views |     created_at      |
+----+----------+-------+---------------------+
|  1 |        1 |  1000 | 2016-04-26 00:00:00 |
|  2 |        2 |   500 | 2016-04-26 00:00:01 |
|  3 |        3 |  2500 | 2016-04-26 00:00:02 |

|  4 |        1 |  1500 | 2016-04-26 02:00:00 |
|  5 |        2 |  1000 | 2016-04-26 02:00:01 |
|  6 |        3 |  3000 | 2016-04-26 02:00:02 |

|  7 |        1 |  5000 | 2016-04-26 04:00:00 |
|  8 |        2 | 10000 | 2016-04-26 04:00:01 |
|  9 |        3 | 30000 | 2016-04-26 04:00:02 |
+----+----------+-------+---------------------+

我现在要做的是获取时间范围内的平均视图值。让我们说,我希望在2小时内获得视频的平均观看次数。我们以video_ 1为例的视频执行此操作

所以我需要做的是以下内容。我需要获得id: 1id: 4的平均值。这将是1250,因为它是(1000 + 1500) / 2。接下来,我需要获得id: 4id: 7的平均值。这将是3250,因为它是(1500 + 5000) / 2。现在,视频在两小时内获得的平均值为2250,因为它是(1250 + 3250) / 2,对吗?

现在我不知道,是如何从MySQL获得这个。甚至可以在普通的MySQL中做到吗?对于很多很多的video_data我需要这个。就像我有超过100小时的数据!如果id: 100id: 105的数据相距不到两个小时,我仍然需要计算这些数据。

我想以某种方式这样做

select *
from `video_data`
where `video_id` in (select `id` from `videos` where `channel_id` = 1)
  and TIMEDIFF(`created_at`, `created_at`) < '02:00:00'

但这只会让我回复每一个结果,因为TIMEDIFF的结果总是00:00:00

我为此

创建了SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE `video_data` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `video_id` int(10) unsigned NOT NULL,
  `shares` int(11) DEFAULT NULL,
  `likes` int(11) DEFAULT NULL,
  `comments` int(11) DEFAULT NULL,
  `total_count` int(11) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `video_data` (`id`, `video_id`, `shares`, `likes`, `comments`, `total_count`, `created_at`, `updated_at`)
VALUES
    (1889612, 245721, 777, 69922, 1314, 72013, '2015-10-04 20:00:04', '2015-10-04 20:00:04'),
    (1896986, 245721, 970, 90611, 1570, 93151, '2015-10-04 21:00:04', '2015-10-04 21:00:04'),
    (1904145, 245721, 1121, 104636, 1725, 107482, '2015-10-04 22:00:05', '2015-10-04 22:00:05'),
    (1911872, 245721, 1199, 115389, 1838, 118426, '2015-10-04 23:00:04', '2015-10-04 23:00:04'),
    (1882621, 245007, 1651, 102569, 2659, 106879, '2015-10-04 19:00:06', '2015-10-04 19:00:06'),
    (1889613, 245007, 1769, 113910, 2775, 118454, '2015-10-04 20:00:05', '2015-10-04 20:00:05'),
    (1896988, 245007, 1829, 121646, 2851, 126326, '2015-10-04 21:00:05', '2015-10-04 21:00:05'),
    (1904150, 245007, 1889, 127677, 2917, 132483, '2015-10-04 22:00:06', '2015-10-04 22:00:06'),
    (1911877, 245007, 1914, 132764, 2957, 137635, '2015-10-04 23:00:05', '2015-10-04 23:00:05'),
    (1845984, 239950, 675, 75030, 1373, 77078, '2015-10-04 12:00:04', '2015-10-04 12:00:04'),
    (1849749, 239950, 857, 97028, 1617, 99502, '2015-10-04 13:00:05', '2015-10-04 13:00:05'),
    (1853996, 239950, 1021, 113648, 1801, 116470, '2015-10-04 14:00:04', '2015-10-04 14:00:04'),
    (1858726, 239950, 1148, 126624, 1919, 129691, '2015-10-04 15:00:04', '2015-10-04 15:00:04'),
    (1863954, 239950, 1297, 137950, 2019, 141266, '2015-10-04 16:00:04', '2015-10-04 16:00:04'),
    (1869723, 239950, 1427, 148069, 2102, 151598, '2015-10-04 17:00:04', '2015-10-04 17:00:04'),
    (1875982, 239950, 1549, 156391, 2194, 160134, '2015-10-04 18:00:05', '2015-10-04 18:00:05'),
    (1882622, 239950, 1618, 161312, 2232, 165162, '2015-10-04 19:00:07', '2015-10-04 19:00:07'),
    (1889616, 239950, 1683, 164783, 2261, 168727, '2015-10-04 20:00:06', '2015-10-04 20:00:06'),
    (1896990, 239950, 1722, 167718, 2278, 171718, '2015-10-04 21:00:06', '2015-10-04 21:00:06'),
    (1904151, 239950, 1743, 170240, 2290, 174273, '2015-10-04 22:00:07', '2015-10-04 22:00:07'),
    (1911880, 239950, 1761, 172363, 2300, 176424, '2015-10-04 23:00:06', '2015-10-04 23:00:06');

我现在执行查询

select avg(pd.shares) AS shares, avg(pd.likes) AS likes, avg(pd.comments) AS comments FROM video_data pd JOIN video_data pd1 ON pd1.video_id = pd.`video_id` AND TIMEDIFF(pd.created_at, pd1.created_at) <= '02:00:00';

+-----------+-------------+-----------+
|  shares   |    likes    | comments  |
+-----------+-------------+-----------+
| 1298.2077 | 123542.5769 | 2032.2769 |
+-----------+-------------+-----------+

但是当查看结果时,likes值似乎是数据库中所有喜欢的平均值,而不仅仅是那些彼此相距仅2小时的值, 对?还是正确的?

2 个答案:

答案 0 :(得分:1)

select t.*,avg(t1.views) from videos t join videos t1 on
t1.video_id=t.video_id 
and timediff(t.created_at,t1.created_at)< '02:00:00' 
group by t.video_id

尝试此查询它应该工作

答案 1 :(得分:0)

  

但这只会返回每个结果,因为TIMEDIFF的结果始终为00:00:00

这样做是因为您使用了相同的列:

TIMEDIFF(`created_at`, `created_at`)

因此几乎不可能使它产生不同的结果。您可能想使用NOW()作为参数之一?