MySQL SELECT 查询返回具有特定月份和年份的记录
mysqlmysqli database
对于特定月份,使用 MONTH();对于年份,使用 YEAR() 方法。让我们首先创建一个表 −
mysql> create table DemoTable ( StudentName varchar(40), StudentAdmissionDate date ); Query OK, 0 rows affected (0.67 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('Chris','2019-01-21'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Robert','2018-09-05'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Mike','2019-09-05'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('David','2019-10-04'); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-------------+----------------------+ | StudentName | StudentAdmissionDate | +-------------+----------------------+ | Chris | 2019-01-21 | | Robert | 2018-09-05 | | Mike | 2019-09-05 | | David | 2019-10-04 | +-------------+----------------------+ 4 rows in set (0.00 sec)
以下查询返回具有特定月份和年份的记录 −
mysql> select *from DemoTable where month(StudentAdmissionDate)=09 and year(StudentAdmissionDate)=2019;
这将产生以下输出 −
+-------------+----------------------+ | StudentName | StudentAdmissionDate | +-------------+----------------------+ | Mike | 2019-09-05 | +-------------+----------------------+ 1 row in set (0.00 sec)