Mysql COUNT按日期只是DATETIME的一部分

时间:2012-08-15 03:07:31

标签: mysql

我有一张如下表格

 +-----+--------------+---------------------+
 | id  | url          | visit_date          | 
 +-----+--------------+---------------------+
 |  1  + 60           | 2012-06-23 18:15:49 |  
 +-----+--------------+---------------------+
 |  2  + 60&ref=qr    | 2012-06-23 12:15:49 |  
 +-----+--------------+---------------------+
 |  3  + 54           | 2012-06-23 18:15:49 |  
 +-----+--------------+---------------------+
 |  4  + 5            | 2012-06-24 18:15:49 |  
 +-----+--------------+---------------------+
 |  5  + 60           | 2012-06-25 13:15:49 |  
 +-----+--------------+---------------------+
 |  6  + 60           | 2012-06-25 19:15:49 |  
 +-----+--------------+---------------------+

我想将结果分组为仅限日期。所以结果就是如果我只想要60的网址。

  +-------------+------------------+
  |visit_date   |count(visit_date) |
  +-------------+------------------+
  |  2012-06-23 |        2         | 
  +-------------+------------------+
  |  2012-06-25 |        2         |
  +-------------+------------------+

我尝试了以下但显然由于时差而得到每个日期。

  SELECT visit_time, COUNT( visit_time ) 
  FROM tracking
  WHERE query_string LIKE  '%60%'
  GROUP BY visit_time

是否可以仅通过忽略时间的日期来做到这一点?

2 个答案:

答案 0 :(得分:4)

是的,你可以。只需使用DATE()功能。

SELECT DATE(visit_time), COUNT( visit_time ) 
FROM tracking
WHERE query_string LIKE  CONCAT('%', '60' ,'%')  -- makes readable
GROUP BY DATE(visit_time)

答案 1 :(得分:0)

使用此

GROUP BY date(visit_time)