MYSQL如何计算数据存在的小时数
MYSQL how to count how many hours where data exists
我正在计算数据库中一天中有多少小时的数据。
我使用这个查询:
SELECT
HOUR(date) AS `hour`, COUNT(date)
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
I got the query from here
示例:
hour COUNT(date)
00 55
01 2
02 33
现在我要计算有多少小时的数据?
上面的示例应该输出值 3,因为在 00、01 和 02
小时有数据
有人想像这样添加 COUNT(小时):
SELECT
HOUR(date) AS `hour`, COUNT(date)**,COUNT(hour)**
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
select count(*)
from
(
SELECT HOUR(date) AS hour
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
) tmp
您正在寻找 count(distinct column)
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count-distinct
SELECT count(distinct HOUR(date)) AS distinct_hour_count
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
我正在计算数据库中一天中有多少小时的数据。
我使用这个查询:
SELECT
HOUR(date) AS `hour`, COUNT(date)
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
I got the query from here
示例:
hour COUNT(date)
00 55
01 2
02 33
现在我要计算有多少小时的数据? 上面的示例应该输出值 3,因为在 00、01 和 02
小时有数据有人想像这样添加 COUNT(小时):
SELECT
HOUR(date) AS `hour`, COUNT(date)**,COUNT(hour)**
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
select count(*)
from
(
SELECT HOUR(date) AS hour
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour
) tmp
您正在寻找 count(distinct column)
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count-distinct
SELECT count(distinct HOUR(date)) AS distinct_hour_count
FROM fb_posts
WHERE DATE(date) = CURDATE() - INTERVAL 1 DAY