Friday 13 January 2012

Inserting the Current Date in a MySQL Table

You can insert the current date into a MySQL table using curdate(), current_date() or current_date:
 
--------------
create table andrews_table (col1 date)
--------------
 
Query OK, 0 rows affected (0.02 sec)
 
--------------
insert into andrews_table select curdate()
--------------
 
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
--------------
insert into andrews_table select current_date()
--------------
 
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
--------------
insert into andrews_table select current_date
--------------
 
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
--------------
insert into andrews_table values (curdate())
--------------
 
Query OK, 1 row affected (0.00 sec)
 
--------------
insert into andrews_table values (current_date())
--------------
 
Query OK, 1 row affected (0.00 sec)
 
--------------
insert into andrews_table values (current_date)
--------------
 
Query OK, 1 row affected (0.00 sec)
 
--------------
select * from andrews_table
--------------
 
+------------+
| col1       |
+------------+
| 2012-01-13 |
| 2012-01-13 |
| 2012-01-13 |
| 2012-01-13 |
| 2012-01-13 |
| 2012-01-13 |
+------------+
6 rows in set (0.00 sec)
 
--------------
drop table andrews_table
--------------
 
Query OK, 0 rows affected (0.00 sec)
 
mysql>

No comments: