- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select items based on value first, then order on the basis of date for rest of the records in MySQL
Use ORDER BY to fix a record and then display
select * from yourTableName order by yourColumnName1=yourValue desc,yourColumnName2;
Let us first create a table −
mysql> create table DemoTable1932 ( UserName varchar(20), ShippingDate date ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1932 values('Chris','2018-10-12'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1932 values('David','2019-04-11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1932 values('Mike','2016-12-04'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1932 values('Carol','2017-12-26'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1932;
This will produce the following output −
+----------+--------------+ | UserName | ShippingDate | +----------+--------------+ | Chris | 2018-10-12 | | David | 2019-04-11 | | Mike | 2016-12-04 | | Carol | 2017-12-26 | +----------+--------------+ 4 rows in set (0.00 sec)
Here is the query to select items based on value first and then order on the basis of date −
mysql> select * from DemoTable1932 order by UserName='David' desc,ShippingDate;
This will produce the following output −
+----------+--------------+ | UserName | ShippingDate | +----------+--------------+ | David | 2019-04-11 | | Mike | 2016-12-04 | | Carol | 2017-12-26 | | Chris | 2018-10-12 | +----------+--------------+ 4 rows in set (0.00 sec)
- Related Articles
- Search records on the basis of date in MySQL?
- Select records from a table on the basis of keywords in MySQL
- Display records on the basis of key-value pairs in MySQL
- MySQL query to ORDER BY records on the basis of modulus result
- How to display the day name on the basis of Date of Birth records in MySQL?
- Perform MySQL UPDATE on the basis of DATE value in another column
- Select and filter the records on month basis in a MySQL table?
- ORDER BY records in MySQL based on a condition
- Fetch records on the basis of LastName using MySQL IN()
- MySQL query to select records from a table on the basis of a particular month number?
- Need to populate autocomplete with records on the basis of first and last name of schools in MySQL?
- Python Pandas - Select first periods of time series data based on a date offset
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- MySQL query for grouping and summing the values based on specific records
- Replace records based on conditions in MySQL?

Advertisements