MySQL SELECT 查询,以返回包含特定月份和年份的记录
对于特定月份,请使用 MONTH() 函数,对于年份,请使用 YEAR() 函数。我们首先创建一个表 −
mysql> create table DemoTable ( StudentName varchar(40), StudentAdmissionDate date ); Query OK, 0 rows affected (0.67 sec)
使用插入命令在表中插入一些记录 −
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)
广告