我想獲取統計信息。Mysql計數和拆分爲DAY,WEEK,YEAR查看統計數據在同一查詢中
我正在努力爲今天,本週,本月計數。
我的查詢:
"SELECT COUNT(id) FROM images i WHERE i.user_id = 3 GROUP BY DAY(i.created_at), WEEK(i.created_at), MONTH(i.created_at)"
輸出:
array(7) {
[0]=>
array(1) {
["COUNT(id)"]=>
string(1) "1"
}
[1]=>
array(1) {
["COUNT(id)"]=>
string(1) "1"
}
[2]=>
array(1) {
["COUNT(id)"]=>
string(1) "5"
}
[3]=>
array(1) {
["COUNT(id)"]=>
string(1) "5"
}
[4]=>
array(1) {
["COUNT(id)"]=>
string(1) "7"
}
[5]=>
array(1) {
["COUNT(id)"]=>
string(2) "17"
}
[6]=>
array(1) {
["COUNT(id)"]=>
string(1) "1"
}
}
但我想分裂,如:
day = 5,
week = 15,
month = 67
怎麼會呢?
完整的工作查詢:
SELECT
COUNT(i.id) AS `all`,
(
SELECT
COUNT(id)
FROM
images
WHERE
user_id = i.user_id AND WEEK(created_at) = WEEK(NOW())
)
AS `week`,
(
SELECT
COUNT(id)
FROM
images
WHERE
user_id = i.user_id AND MONTH(created_at) = MONTH(NOW())
)
AS `month`,
(
SELECT
COUNT(id)
FROM
images
WHERE
user_id = i.user_id AND DAY(created_at) = DAY(NOW())
)
AS `day`
FROM
images i
WHERE
i.user_id = " . $user->getId()
你怎麼會知道這天/周/月是哪個? – Strawberry 2014-12-03 09:44:54
我只想知道,今天,本週,本月(當前日期統計。) – 2014-12-03 09:48:11
目前是否也是最新的? – Strawberry 2014-12-03 09:49:22