Wed, April 14, 2021
 
https://devcodepro.comdevcodepro
Home · Latest · Trends  /  SQL
2

MySQL COUNT today week month

SELECT
    *,
    COUNT(IF(date >= curdate(), 1, NULL)) 'todaycount',
    COUNT(IF(yearweek(date) = yearweek(curdate()), 1, NULL)) 'weekcount',
    COUNT(IF(month(date) = month(curdate()), 1, NULL)) 'monthcount',
    COUNT(IF(date <= curdate(), 1, NULL)) 'allcount' 
FROM
    mytable
rated 2 times (2) (0)
comments: 1 / hits: 215  / 4 months ago, wed, jan 6, 21, 02:42:50
More From » SQL
 

Comments

1
How about:

Code:
SELECT
*,
SUM(date >= curdate()) as 'todaycount',
SUM(yearweek(date) = yearweek(curdate())) as 'weekcount',
SUM(month(date) = month(curdate())) as 'monthcount',
SUM(date <= curdate()) as 'allcount'
FROM
mytable
[#56] Sunday, January 10, 2021, 4:03:20
 
suppe
commented 3 months ago
Only authorized users can post. Please sign in first, or register a free account
 
Share
Posted
Jake
Member since Jan 6, 2021
Total Code Snippets: 4
Total Comments: 0
Location: Hartford, CT
Jake snippets
3 months ago, sun, jan 24, 21, 5:39:27
comments: 0 / hits: 74
CSS
3 months ago, mon, jan 25, 21, 10:13:23
comments: 0 / hits: 76
PHP
2 months ago, tue, feb 23, 21, 1:51:57
comments: 0 / hits: 86
PHP