我有一张像附件一样的桌子。
我想找到“某个月中数据库中包含最多条目的项目数”。
例如,Jan有10个条目,2月有13个条目,Mar有8个条目。
我想从数据库中找到2月号13,因为它有最大条目。如何查看查询中的时间范围?
答案 0 :(得分:1)
您可以按月和按年对所有重新分组进行分组,以获得这样的计数:
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)
这会给你这样的东西:
+--------+--------+--------+
| month | year | number |
+--------+--------+--------+
| 1 | 2013 | 13 |
| 2 | 2013 | 8 |
然后你可以选择这样的最大值:
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
哪位能给你:
+--------+--------+--------+
| month | year | number |
+--------+--------+--------+
| 4 | 2013 | 19 |
+--------+--------+--------+
哪个代表最高月份
答案 1 :(得分:0)
SELECT COUNT(*) FROM `mytable` WHERE releasedate >= DATE '2013-02-01' AND releasedate <= DATE '2013-02-28'
那应该有用
修改强>
正如lafor所建议的那样......
WHERE YEAR(releasedate)=2013 AND MONTH(releasedate)=2
也应该有用