我有一個表格:userid和時間戳每次用戶打開一個頁面時會插入一個新字段。mysql得到小時/分鐘/秒的總和
我試圖得到小時總量/分鐘/天/周出現在爲多個用戶1個月的間隔。
我試了一堆不同的查詢,但每個都結束了非常低效率。
理想情況下,我想喜歡的東西來結束:
userid | minutes | hours | days | weeks
1 10080 168 7 1
2 1440 24 1 0
希望有人可以闡明如何做到這一點一些輕。
下面是我試過的查詢:
SELECT
w.time AS `week`,
d.time AS `day`,
h.time AS `hour`,
m.time AS `minutes`
FROM (
SELECT
SUM(t.time) AS `time`
FROM (
SELECT
COUNT(DISTINCT WEEK(`timestamp`)) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB(NOW() , INTERVAL 1 MONTH)
GROUP BY MONTH(`timestamp`)
) t
) w,
(
SELECT
SUM(t.time) AS `time`
FROM (
SELECT
COUNT(DISTINCT DAY(`timestamp`)) AS `time`
FROM table
WHERE
userid = "52"
AND
`timestamp` > DATE_SUB(NOW() , INTERVAL 1 MONTH)
GROUP BY MONTH(`timestamp`)
) t
) d,
(
SELECT
SUM(t.timestamp) AS `time`
FROM (
SELECT
COUNT(DISTINCT HOUR(`timestamp`)) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB(NOW() , INTERVAL 1 MONTH)
GROUP BY DAY(`timestamp`)
) t
) h,
(
SELECT
SUM(t.timestamp) AS `time`
FROM (
SELECT
COUNT(DISTINCT MINUTE(`timestamp`)) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB(NOW() , INTERVAL 1 MONTH)
GROUP BY HOUR(`timestamp`)
) t
) m
似乎非常過分的這個任務,也許有人有更好的東西?
你試過什麼疑問?我很難理解你的源數據。 – 2012-07-27 03:43:25
'效率低下' - 你如何衡量效率? – zerkms 2012-07-27 03:44:28
我剛剛考慮問題並創建了一些奇怪的查詢。對不起,我沒有打擾他們添加他們,因爲他們是異乎尋常的 – Corey 2012-07-27 04:23:15