Date-Based Sorting : DATE « Date Time « SQL / MySQL






Date-Based Sorting

    
mysql>
mysql> CREATE TABLE temporal_val
    -> (
    ->  d       DATE,
    ->  dt      DATETIME,
    ->  t       TIME,
    ->  ts      TIMESTAMP
    -> );
mysql>
mysql> INSERT INTO temporal_val (d, dt, t, ts)
    ->  VALUES
    ->          ('1970-01-01','1884-01-01 12:00:00','13:00:00','1980-01-01 02:00:00'),
    ->          ('1999-01-01','1860-01-01 12:00:00','19:00:00','2021-01-01 03:00:00'),
    ->          ('1981-01-01','1871-01-01 12:00:00','03:00:00','1975-01-01 04:00:00'),
    ->          ('1964-01-01','1899-01-01 12:00:00','01:00:00','1985-01-01 05:00:00')
    -> ;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT * FROM temporal_val;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
+------------+---------------------+----------+---------------------+
12 rows in set (0.00 sec)

mysql>
mysql> SELECT * FROM temporal_val ORDER BY d;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
| 1964-01-01 | 1899-01-01 12:00:00 | 01:00:00 | 1985-01-01 05:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1970-01-01 | 1884-01-01 12:00:00 | 13:00:00 | 1980-01-01 02:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1981-01-01 | 1871-01-01 12:00:00 | 03:00:00 | 1975-01-01 04:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
| 1999-01-01 | 1860-01-01 12:00:00 | 19:00:00 | 2021-01-01 03:00:00 |
+------------+---------------------+----------+---------------------+
12 rows in set (0.00 sec)

mysql>
mysql> drop table temporal_val;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
  








Related examples in the same category

1.Extract just the date: DATE()
2.Treating Dates or Times as Numbers
3.Synthesizing Dates or Times Using Formatting Functions
4.Synthesizing Dates or Times Using Component-Extraction Functions
5.Date-Based Summaries
6.Add an incorrect date to a special table and show the result.