取回带有年和月的日期的 MySQL 查询?
对于年和月日期取回,您可以在 MySQL 中使用 YEAR() 和 MONTH() 函数。我们先创建一个表 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ShippingDate datetime ); Query OK, 0 rows affected (0.48 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable(ShippingDate) values('2019-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(ShippingDate) values('2018-12-01'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(ShippingDate) values('2019-06-02'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(ShippingDate) values('2019-11-18'); Query OK, 1 row affected (0.10 sec)
使用选择语句从表中显示所有记录 -
mysql> select *from DemoTable;
输出
+----+-----------------------+ | Id | ShippingDate | +----+-----------------------+ | 1 | 2019-01-31 00 :00 :00 | | 2 | 2018-12-01 00 :00 :00 | | 3 | 2019-06-02 00 :00 :00 | | 4 | 2019-11-18 00 :00 :00 | +----+-----------------------+ 4 rows in set (0.00 sec)
以下是用于根据特定年和月取回日期的查询 -
mysql> select *from DemoTable where ( year(ShippingDate)='2019') and (month(ShippingDate)='06');
输出
+----+-----------------------+ | Id | ShippingDate | +----+-----------------------+ | 3 | 2019-06-02 00 :00 :00 | +----+-----------------------+ 1 row in set (0.07 sec)
广告