简体   繁体   English

MYSQL:计算具有相同日期的行

[英]MYSQL: count rows with same date

I got a table called event_details summarized as below: 我得到了一个名为event_details的表,总结如下:

Id creation_date
 1 2015-03-18 18:59
 2 2015-03-18 17:59
 3 2015-03-18 16:59
 4 2015-03-17 14:59
 5 2015-03-17 18:59
 6 2015-03-17 19:59
 7 2015-03-16 11:59
 8 2015-03-15 03:59
 9 2015-03-15 02:59
10 2015-03-15 08:59
11 2015-03-14 09:59

I want to count rows with the same date and get result like below? 我想对具有相同日期的行进行计数,并得到如下所示的结果?

How can I do this? 我怎样才能做到这一点? Is it possible using nested queries or I should use joins? 是否可以使用嵌套查询或我应该使用联接?

cnt creation_date
  3 2015-03-18
  3 2015-03-17
  1 2015-03-16
  3 2015-03-15
  1 2015-03-14

And a final question: Is this naturally a heavy SQL task and impose high loads to database-server or not? 最后一个问题:这自然是一项繁重的SQL任务,是否对数据库服务器施加了高负载?

This could be done with group by and count 这可以通过group bycount来完成

select 
count(*) as cnt,
date(creation_date) as creation_date_date
from event_details
group by creation_date_date

You can try this. 你可以试试看

SELECT COUNT(*), creation_date FROM table_name GROUP BY creation_date

This first groups all your rows with the same creation_date and then counts the number of rows in each group. 这首先将所有具有相同的creation_date的行分组,然后计算每个组中的行数。 You can furthermore order the rows as follows: 您还可以按以下顺序对行进行排序:

SELECT COUNT(*), creation_date FROM table_name GROUP BY creation_date ORDER BY creation_date

This does it. 做到了。

SELECT COUNT(*) as cnt,
       date(creation_date) as creation_date 
FROM event_details 
GROUP BY 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM