
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?
Let us first create a table &mnus;
mysql> create table DemoTable ( `timestamp` timestamp ); Query OK, 0 rows affected (1.12 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(now()); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('00:00:00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2018-01-10 12:34:45'); Query OK, 1 row affected (0.80 sec) mysql> insert into DemoTable values('2019-12-31 10:50:45'); Query OK, 1 row affected (0.84 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+ | timestamp | +---------------------+ | 2019-08-17 06:17:12 | | 0000-00-00 00:00:00 | | 2018-01-10 12:34:45 | | 2019-12-31 10:50:45 | +---------------------+ 4 rows in set (0.00 sec)
Following is the query to order the timestamp in descending order by first displaying the 0 timestamp −
mysql> select *from DemoTable order by (`timestamp`=0) DESC,`timestamp` DESC;
This will produce the following output −
+---------------------+ | timestamp | +---------------------+ | 0000-00-00 00:00:00 | | 2019-12-31 10:50:45 | | 2019-08-17 06:17:12 | | 2018-01-10 12:34:45 | +---------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Create MySQL datetime column with default 0000-00-00?
- How can I store ‘0000-00-00’ as a date in MySQL?
- MySQL query to select date from 00:00 to today’s date
- In which format Year(2) or Year(4) MySQL will return the value of year from date ‘0000-00-00’?
- Convert PHP variable “11:00 AM” to MySQL time format?
- Display hour in KK (00-11) format in Java
- Format hour in HH (00-23) format in Java
- Construct ∈-NFA of Regular Language L = (00)*1(11)*
- Construct ∈-NFA of Regular Language L = (0+1)*(00+ 11)
- 8085 Program to alternately display 00 and FF in the data field
- Program to alternately display 00 and FF in the data field in 8085 Microprocessor
- How to order by timestamp (descending order) in MongoDB
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- MySQL command to order timestamp values in ascending order?
- How to order by timestamp in MySQL?

Advertisements