使用 MySQL 获取当前日期注册人数
为此,可以使用 COUNT() 和 GROUP BY 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 ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1819 values('Chris','2019-11-29 12:30:34'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1819 values('David','2019-11-30 12:30:34'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1819 values('Mike','2019-12-30 12:30:34'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1819 values('Sam','2018-11-30 11:00:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1819 values('Carol','2019-11-30 10:00:00'); Query OK, 1 row affected (0.00 sec)
使用 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)
广告