I have a simple MySQL table like this:
create table `users`(
`id` int(5) NOT NULL AUTO_INCREMENT ,
`user` varchar(100) NOT NULL ,
`registration_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`)
)
and I’m wondering how should I get the count of users that have been registered:
- today
- this week
- this month
and is there any way of doing this in a single query ?
Assuming Monday is the start of the week, use:
Though, because of using FROM_UNIXTIME this won’t be able to use an index if one existed on the
registration_datecolumn. You could though, if you reversed the logic to apply the UNIX_TIMESTAMP function to the values for the dates being filtered against.