简体   繁体   中英

SQL get nearest date record

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

You need SORT desc function on column start_Date . Below is the query which will produce your desired result.

select * from table1
order by Start_Date desc;

You can check sqlfiddle demo here

If the dates are in future, you have to use asc to get your desired result.

select * from table1
order by Start_Date asc;

If your dates are mix of Past and future dates like below sample data.

ID Name   Start_Date
---------------------
1  abc   2018-01-01
2  efg   2018-02-05
3  pqr   2018-01-16
4  xyz   2018-02-19
1  abc   2017-01-01
2  efg   2017-02-05
3  pqr   2017-01-16
4  xyz   2017-02-19

Below query can be a option to show data in more friendly format.

select * from (
select * from table1
where start_date < current_date
order by start_date desc
) as B
union
select 0,'TODAY_DATE', current_date
union
select * from (
select * from table1
where start_date > current_date
order by start_date asc
) as A 

It will sort past dates data in desc order, then add TODAY date to result and then add future data in asc format as below.

 ID  Name        Start_Date
--------------------------
4   xyz         2017-02-19
2   efg         2017-02-05
3   pqr         2017-01-16
1   abc         2017-01-01
0   TODAY_DATE  2017-08-18
1   abc         2018-01-01
3   pqr         2018-01-16
2   efg         2018-02-05
4   xyz         2018-02-19

check SQLfiddle demo here

Use ORDER BY function of sql. Like this:

SELECT * 
FROM 
     table_name 
ORDER BY 
   start_date DESC;

As per my understanding below would be your query, let me know further.

Use can use Order by with ASC|Desc based on requirement,

select * from booking_table order by start_date DESC;

You want nearest date from todate so you can try followuing query

SELECT * FROM table 
WHERE start_date >= now()
ORDER BY start_date ASC;

OR

If you want it in revere order then:

SELECT * FROM table 
WHERE start_date <= now()
ORDER BY start_date DESC;

You can use the following query:

SELECT Booking_id, Name, start_date
FROM mytable
ORDER BY ABS(DATEDIFF(start_date, NOW()));

The ORDER BY clause sorts by the distance in days from today's date. The date having the smallest distance comes first.

Demo here

这对你有用

select * from table_name Order By start_date Desc;

Based on one of your comments:

today's records follow by future records and then old records at the end

this will sort today and future dates first, followed by past dates:

ORDER BY 
   CASE WHEN start_date >= CURRENT_DATE THEN 1 ELSE 2 END,
   start_date

Results in both new and old dates sorted ascending, if you want the old dates sorted descending:

ORDER BY 
   CASE WHEN start_date >= CURRENT_DATE THEN 1 ELSE 2 END,
   ABS(CURRENT_DATE - start_date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM