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)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP