使用MySQL获取当前日期正在注册的人数
为此,您可以COUNT()
与GROUPBY一起使用MONTH()
。要与当前日期匹配,请使用CURRENT_DATE()。当前日期如下-
mysql> select curdate() ; +------------+ | curdate() | +------------+ | 2019-11-30 | +------------+ 1 row in set (0.00 sec)
让我们首先创建一个表-
mysql> create table DemoTable1819 ( Name varchar(20), RegisteringDate datetime );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1819 values('Chris','2019-11-29 12:30:34'); mysql> insert into DemoTable1819 values('David','2019-11-30 12:30:34'); mysql> insert into DemoTable1819 values('Mike','2019-12-30 12:30:34'); mysql> insert into DemoTable1819 values('Sam','2018-11-30 11:00:00'); mysql> insert into DemoTable1819 values('Carol','2019-11-30 10:00:00');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1819;
这将产生以下输出-
+-------+---------------------+ | Name | RegisteringDate | +-------+---------------------+ | Chris | 2019-11-29 12:30:34 | | David | 2019-11-30 12:30:34 | | Mike | 2019-12-30 12:30:34 | | Sam | 2018-11-30 11:00:00 | | Carol | 2019-11-30 10:00:00 | +-------+---------------------+ 5 rows in set (0.00 sec)
这是查询以获取当前日期有多少人注册的查询-
mysql> select count(RegisteringDate) as Output from DemoTable1819 WHERE DATE(RegisteringDate) = CURDATE() GROUP BY MONTH(RegisteringDate);
这将产生以下输出-
+--------+ | Output | +--------+ | 2 | +--------+ 1 row in set (0.00 sec)