如何按月用 MySQL 進行 SELECT?


如需按月進行選擇,請使用 MONTH() 函式。我們首先建立一個表 -

mysql> create table DemoTable1599
   -> (
   -> Shippingdate datetime
   -> );
Query OK, 0 rows affected (0.78 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable1599 values('2019-10-21');
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable1599 values('2018-12-12');
Query OK, 1 row affected (0.35 sec)
mysql> insert into DemoTable1599 values('2015-11-21');
Query OK, 1 row affected (0.34 sec)
mysql> insert into DemoTable1599 values('2017-12-31');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1599 values('2018-12-26');
Query OK, 1 row affected (0.17 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select * from DemoTable1599;

這將產生以下輸出 -

+---------------------+
| Shippingdate        |
+---------------------+
| 2019-10-21 00:00:00 |
| 2018-12-12 00:00:00 |
| 2015-11-21 00:00:00 |
| 2017-12-31 00:00:00 |
| 2018-12-26 00:00:00 |
+---------------------+
5 rows in set (0.00 sec)

以下是按月進行 SELECT 的查詢 -

mysql> select * from DemoTable1599
   -> where month(Shippingdate)=12;

這將產生以下輸出 -

+---------------------+
| Shippingdate        |
+---------------------+
| 2018-12-12 00:00:00 |
| 2017-12-31 00:00:00 |
| 2018-12-26 00:00:00 |
+---------------------+
3 rows in set (0.00 sec)

更新於: 16-12-2019

377 次瀏覽

開啟你的 事業

完成課程以獲得認證

開始
廣告
© . All rights reserved.