Create a temporary table with dates in MySQL


To create a temporary table with dates, use the CREATE TEMPORARY TABLE in MySQL. Following is the syntax −

Syntax

create temporary table yourTableName(
   yourColumnName datetime
);

Let us first create a table −

mysql> create temporary table DemoTable
   -> (
   -> DueDate datetime
   -> );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(now());
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable values(curdate());
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable values('2018-01-21');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable values('2019-12-31');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------+
| DueDate             |
+---------------------+
| 2019-10-27 18:07:41 |
| 2019-10-27 00:00:00 |
| 2018-01-21 00:00:00 |
| 2019-12-31 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)

Updated on: 13-Dec-2019

525 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements