讓我們假設我有下表,稱爲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: 1
和id: 4
的平均值。這將是1250
,因爲它是(1000 + 1500)/2
。接下來我需要得到id: 4
和id: 7
的平均值。這將是3250
,因爲它是(1500 + 5000)/2
。現在,視頻在兩個小時內的平均分數將爲2250
,因爲它是(1250 + 3250)/2
,對嗎?
現在我不知道,是如何從MySQL獲得這個。它甚至可以在普通的MySQL中執行嗎?我需要很多很多video_data。就像我有超過100個小時的數據!如果id: 100
和id: 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
值的結果時是ALL喜歡在數據庫中的平均水平,而不僅僅是那些只有誰是2相隔幾小時,對吧?或者它是正確的?
「正確的嗎?」我們怎麼知道? – Strawberry
ID 4和ID 7似乎是在相同的日期爲同一個視頻,所以我很困惑,爲什麼這些將是單獨的行。 – Strawberry
沒有channel_id,也沒有明確說明如何定義時間範圍。 – Strawberry