How to fetch data from DB order by datetime closest to now or current date.
Below is my query, this will give idea about table structure.
SELECT o.id,
p.price_quoted,
p.discount,
p.offer_price,
p.amount_paid,
o.user_id,
s.subject,
o.page_count,
o.deadline,
o.order_status,
u.timezone
FROM orders o
JOIN payment p
ON p.order_id = o.id
JOIN subjects s
ON s.id = o.subject_id
JOIN users u
ON u.id = o.user_id
ORDER BY o.deadline
ORDER BY o.deadline is doing the work but I need to handle the case where deadline has already passed.
Suppose today's date is "2021-05-08" and in my database I have 3 records as below:
deadline |
---|
2021-05-01 |
2021-05-22 |
2021-05-10 |
I want to get as below from Database:
deadline |
---|
2021-05-10 |
2021-05-22 |
2021-05-01 |
As 2021-05-01 is past, so it should come in last and future dates 2021-05-10, 2021-05-22 should be sorted as closest to today's date.
I am using MySQL and PHP.
Thanks in advance.