如何在 MySQL 表格的一列中自動插入當前日期?


藉助 CURDATE() 和 NOW() 函式,我們可以在 MySQL 表格中的一列中自動插入當前日期。

例項

假設我們要在 year_testing 表格的 OrderDate 列中自動插入當前日期,則以下查詢將執行此操作 −

mysql> Insert into year_testing (OrderDate) Values(CURDATE());
Query OK, 1 row affected (0.11 sec)
mysql> Select * from year_testing;
+------------+
| OrderDate  |
+------------+
| 2017-10-28 |
+------------+
1 row in set (0.00 sec)

mysql> Insert into year_testing (OrderDate) Values(NOW());
Query OK, 1 row affected, 1 warning (0.12 sec)

mysql> Select * from year_testing;
+------------+
| OrderDate  |
+------------+
| 2017-10-28 |
| 2017-10-28 |
+------------+
2 rows in set (0.00 sec)

更新於: 19-6-2020

4k+ 瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.