在 MySQL 中调用 NOW() 函数以获取当前日期记录?


我们先创建一个表 -

mysql> create table DemoTable
-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ShippingDate datetime
-> );
Query OK, 0 rows affected (1.16 sec)

使用 insert 命令在表中插入一些记录。考虑当前日期“2019-06-28” -

mysql> insert into DemoTable(ShippingDate) values('2019-01-31');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(ShippingDate) values('2019-06-06');
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable(ShippingDate) values('2019-06-28');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable(ShippingDate) values('2019-07-01');
Query OK, 1 row affected (0.16 sec)

使用 select 语句显示表中的所有记录 -

mysql> select *from DemoTable;

输出

这会生成以下输出 -

+----+---------------------+
| Id | ShippingDate        |      
+----+---------------------+
| 1  | 2019-01-31 00:00:00 |
| 2  | 2019-06-06 00:00:00 |
| 3  | 2019-06-28 00:00:00 |
| 4  | 2019-07-01 00:00:00 |
+----+---------------------+
4 rows in set (0.00 sec)

这里有在 MySQL 中调用 NOW() 函数并从表中获取当前日期记录的查询 -

mysql> select *from DemoTable where Date(ShippingDate)=Date(now());

输出

这会生成以下输出 -

+----+---------------------+
| Id | ShippingDate        |
+----+---------------------+
| 3  | 2019-06-28 00:00:00 |
+----+---------------------+
1 row in set (0.00 sec)

更新于: 30-6-2020

359 浏览

Kickstart Your Career

完成课程以获得认证

开始
Advertisements