php - SQL get nearest date record
872
This is an sample data:
Booking_id Name start_date
1 abc 1/1/2018
2 efg 5/2/2018
3 pqr 16/1/2018
4 xyz 19/2/2018
I want this is to be in order nearest to today date on top and past date in last
Answer
Solution:
You need
{-code-2}
function on column{-code-3}
. Below is the query which will produce your desired result.You can check sqlfiddle demo here
If the dates are in future, you have to use
{-code-5}
to get your desired result.If your dates are mix of Past and future dates like below sample data.
Below query can be a option to show data in more friendly format.
It will sort past dates data in
{-code-2}
order, then add{-code-10}
date to result and then add future data in{-code-5}
format as below.Answer
check SQLfiddle demo here
Answer
Answer
Answer
Solution:
You can use the following query:
The
ORDER BY
clause sorts by the distance in days from today's date. The date having the smallest distance comes first.Answer
Solution:
Use
ORDER BY
function of sql. Like this:Answer
Solution:
As per my understanding below would be your query, let me know further.
Use can use Order by with ASC|Desc based on requirement,
Answer
Solution:
You want nearest date from todate so you can try followuing query
OR
If you want it in revere order then:
Answer
Solution:
this works for you,
select * from table_name Order By start_date Desc;
Answer
Solution:
Based on one of your comments:
this will sort today and future dates first, followed by past dates:
Results in both new and old dates sorted ascending, if you want the old dates sorted descending: