6

我有一张这样的桌子

CREATE TABLE news
(
Id         INT NOT NULL auto_increment,
Headline   VARCHAR (255) NULL,
InDateTime   DATETIME NULL
)

如何获取每秒记录数(InDateTime)?

我在用着Mysql


样本记录:

578921,   'headline1', '8/20/2012 12:01:53 PM' 
578922,   'headline2', '8/20/2012 12:01:53 PM' 
578923,   'headline3', '8/20/2012 12:01:53 PM' 
578924,   'headline4', '8/20/2012 12:01:59 PM' 
578925,   'headline5', '8/20/2012 12:01:59 PM' 
578926,   'headline6', '8/20/2012 12:01:59 PM' 
578927,   'headline7', '8/20/2012 12:01:59 PM' 
578928,   'headline8', '8/20/2012 12:02:03 PM' 

预期输出:

time,                    count
'8/20/2012 12:01:53 PM', 3
'8/20/2012 12:01:59 PM', 4
'8/20/2012 12:02:03 PM', 1 
4

4 回答 4

9

你厌倦了这个:

SELECT COUNT(id), InDateTime
FROM news
GROUP BY InDateTime
于 2012-09-12T05:58:46.240 回答
2

在这里,您要根据time. 对于每个time您想要的行数。所以你可以使用这个查询。

select time, count(*)
from news
group by time

这里group by time将创建单独的一组不同的time值。 select time将在第一列中选​​择时间。并将count(*)给出包含该值的行数。

你最好读这个

于 2012-09-12T06:19:51.647 回答
1
SELECT InDateTime AS Time, COUNT(InDateTime) AS Count
FROM NEWS
GROUP BY InDateTime

结果

Time                     Count
2012-08-20 12:01:53.000   3
2012-08-20 12:01:59.000   4
2012-08-20 12:02:03.000   1
于 2012-09-12T06:24:18.797 回答
1

任何转换的最简单方法

SELECT `time`, COUNT(*) totalCount
FROM tableName
GROUP by `time
于 2012-09-12T06:08:37.653 回答