查詢某個特定月中所有記錄的 MySQL 查詢
若要查詢 MySQL 中某個特定月中所有記錄,可以使用 monthname() 或 month() 函式。
語法如下。
select *from yourTableName where monthname(yourColumnName)='yourMonthName';
為了理解上述語法,讓我們建立一個表。建立表的查詢如下
mysql> create table selectAllEntriesDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.63 sec)
使用 insert 命令向表中插入一些記錄。
查詢如下
mysql> insert into selectAllEntriesDemo(ShippingDate) values('2019-01-21'); Query OK, 1 row affected (0.24 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2018-02-24'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2010-10-22'); Query OK, 1 row affected (0.20 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2011-04-12'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2013-02-10'); Query OK, 1 row affected (0.18 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2014-02-15'); Query OK, 1 row affected (0.16 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2016-06-14'); Query OK, 1 row affected (0.18 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2017-02-14'); Query OK, 1 row affected (0.51 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2015-03-29'); Query OK, 1 row affected (0.19 sec)
使用 select 語句顯示錶中的所有記錄。
查詢如下。
mysql> select *from selectAllEntriesDemo;
以下為輸出。
+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 1 | 2019-01-21 00:00:00 | | 2 | 2018-02-24 00:00:00 | | 3 | 2010-10-22 00:00:00 | | 4 | 2011-04-12 00:00:00 | | 5 | 2013-02-10 00:00:00 | | 6 | 2014-02-15 00:00:00 | | 7 | 2016-06-14 00:00:00 | | 8 | 2017-02-14 00:00:00 | | 9 | 2015-03-29 00:00:00 | +----+---------------------+ 9 rows in set (0.00 sec)
以下為查詢某個特定月中所有記錄的查詢
mysql> select *from selectAllEntriesDemo where monthname(ShippingDate)='February';
以下為輸出。
+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 2 | 2018-02-24 00:00:00 | | 5 | 2013-02-10 00:00:00 | | 6 | 2014-02-15 00:00:00 | | 8 | 2017-02-14 00:00:00 | +----+---------------------+ 4 rows in set (0.00 sec)
以下為備用查詢。
mysql> select *from selectAllEntriesDemo where month(ShippingDate)=2;
以下為輸出。
+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 2 | 2018-02-24 00:00:00 | | 5 | 2013-02-10 00:00:00 | | 6 | 2014-02-15 00:00:00 | | 8 | 2017-02-14 00:00:00 | +----+---------------------+ 4 rows in set (0.04 sec)
廣告