我們如何在 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.