19

示例表结构:

+-------------------------+---------------+------+-----+-----------------------+-----------------------------+
| Field                   | Type          | Null | Key | Default               | Extra                       |
+-------------------------+---------------+------+-----+-----------------------+-----------------------------+
| id                      | char(25)      | NO   | PRI | NULL                  |                             |
| created_at              | timestamp     | NO   |     | CURRENT_TIMESTAMP     | on update CURRENT_TIMESTAMP |
+-------------------------+---------------+------+-----+-----------------------+-----------------------------+

如何找出每天添加的平均行数?

4

3 回答 3

36

这将返回一个int数字:

select count(*) / count(distinct date(created_at))
from your_table

要获取decimal数字,请使用:

select count(*) * 1.0 / count(distinct date(created_at))
from your_table
于 2013-10-28T14:44:55.417 回答
27
SELECT AVG(rowsPerDay) AS avgPerDay
FROM ( SELECT 
         COUNT(*) AS rowsPerDay
       FROM tbl
       GROUP BY DATE(created_at)
     ) AS a
;
于 2013-10-28T14:44:43.103 回答
1

如果有天数为 0 行,这些都不起作用,对吗?我想如果您每天都需要,您可以计算自第一天以来的天数并使用它

这适用于 MariaDB:

SELECT COUNT(*) / DATEDIFF ( CURRENT_DATE() , MIN(DATE(created_at)) ) AS "Avg. rows per day" 
FROM your_table;
于 2020-08-28T14:53:11.203 回答