
- 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 by current day and month?
For this, you can use the ORDER BY CASE statement. Let us first create a table −
mysql> create table DemoTable ( DueDate date ); Query OK, 0 rows affected (0.56 sec)
Note − Let’s say the current date is 2019-07-22.
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-03-10'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-08-25'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-06-01'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2019-12-31'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+ | DueDate | +------------+ | 2019-03-10 | | 2019-08-25 | | 2019-06-01 | | 2019-12-31 | +------------+ 4 rows in set (0.00 sec)
Following is the query to order by day and month −
mysql> select *from DemoTable order by case when DueDate > curdate() then 0 else 1 end, DueDate;
This will produce the following output −
+------------+ | DueDate | +------------+ | 2019-08-25 | | 2019-12-31 | | 2019-03-10 | | 2019-06-01 | +------------+ 4 rows in set (0.00 sec)
- Related Articles
- Filter the records of current day, month and year in MySQL?
- How to get last day of the current month in MySQL?
- MongoDB query to search for Month and Day only?
- How to get the first day of the current month in MySQL?
- How to compare Year, Month and Day in a MySQL query and display matching records
- Fetch date records comparing with the current date’s day and month in MySQL
- How to get current day, month and year in Java 8?
- MySQL query to order by NULL values
- MongoDB query to search date records using only Month and Day
- MySQL query to order and display difference between dates from the current date
- MySQL query to get result by month and year based on condition?
- Format MySQL date and convert to year-month-day
- MySQL query to order by two fields and NULL values in chronological order?
- Order MySQL query by multiple ids?
- MySQL query to select distinct order by id

Advertisements