[英]MySQL query to retrieve maximum number of items within a given time period
I've a table like attached. 我有一张桌子。
I want to find the "number of items in a certain month which has maximum entries in the database". 我想找到“某月在数据库中具有最大条目数的项目数”。
For instance, Jan has 10 entries, Feb has 13 entries, Mar has 8 entries. 例如,一月有10个条目,二月有13个条目,三月有8个条目。
I want to find the the number 13 for Feb from the database as it has the max entries. 我想从数据库中查找2月的数字13,因为它具有最大条目数。 How do I check the time range in the query?
如何检查查询中的时间范围?
You can group all of your realeasedates by month and year to get a count like this: 您可以按月和年对所有已完成的工作进行分组,以得到如下计数:
SELECT MONTH(releasedate) AS month, YEAR(releasedate) as year, count(r_id) AS number
FROM my_table
GROUP BY YEAR(releasedate), MONTH(releasedate)
ORDER BY YEAR(releasedate), MONTH(releasedate)
This'll give you something like this: 这会给你这样的东西:
+--------+--------+--------+
| month | year | number |
+--------+--------+--------+
| 1 | 2013 | 13 |
| 2 | 2013 | 8 |
Then you could select the maximum like this: 然后您可以选择最大值:
SELECT MONTH(releasedate) AS month, YEAR(releasedate) as year, count(r_id) AS number
FROM my_table
GROUP BY YEAR(releasedate), MONTH(releasedate)
ORDER BY count(r_id)
LIMIT 1
Which'll give you: 这会给你:
+--------+--------+--------+
| month | year | number |
+--------+--------+--------+
| 4 | 2013 | 19 |
+--------+--------+--------+
Which'll represent the highest month 哪个代表最高月份
SELECT COUNT(*) FROM `mytable` WHERE releasedate >= DATE '2013-02-01' AND releasedate <= DATE '2013-02-28'
That should work 那应该工作
EDIT: 编辑:
As suggested by lafor... 如lafor所建议...
WHERE YEAR(releasedate)=2013 AND MONTH(releasedate)=2
should also work 应该也可以
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.