按时间戳字段的日期部分分组

时间:2014-03-24 10:00:32

标签: mysql group-by timestamp

我有users表。我想生成当天加入的用户的报告。 问题是,我的DateJoined字段是timestamp字段,

查询如下:

SELECT COUNT(UserID) AS TOT FROM users GROUP BY DateJoined

不起作用,如何将其GROUP BY仅用于日期部分,而不是DateJoined字段的时间?

3 个答案:

答案 0 :(得分:12)

您的代码:

SELECT 
      COUNT(UserID) AS TOT 
      FROM users 
      GROUP BY DateJoined

您应该改为:

SELECT 
       DATE(DateJoined), 
       COUNT(UserID) AS TOT 
       FROM users 
       GROUP BY DATE(DateJoined)

sqlfiddle

答案 1 :(得分:1)

尝试这样

 SELECT COUNT(UserID) AS TOT FROM users GROUP BY CAST(DateJoined AS DATE)

OR

SELECT COUNT(UserID) AS TOT, FROM a_table GROUP BY DATE(DateJoined );

答案 2 :(得分:1)

使用像这样的查询

SELECT COUNT(UserID) AS TOT FROM users GROUP BY DATE(DateJoined)