1

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

Paritosh Mahale
  • 1,238
  • 2
  • 14
  • 42

7 Answers7

5

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

zarruq
  • 2,445
  • 2
  • 10
  • 19
3

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

Giorgos Betsos
  • 71,379
  • 9
  • 63
  • 98
2

Use ORDER BY function of sql. Like this:

SELECT * 
FROM 
     table_name 
ORDER BY 
   start_date DESC;
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
1

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;
Sam
  • 1,106
  • 10
  • 14
  • 1
    this return record in reverse order by date not the nearest record to today's date – Paritosh Mahale Aug 18 '17 at 05:36
  • Dear Paritosh, Reverse order from today means the same it will render the nearest records in descending order to today's date. – Sam Oct 16 '17 at 06:37
1

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;
B. Desai
  • 16,414
  • 5
  • 26
  • 47
1

this works for you,

select * from table_name Order By start_date Desc;

Sanjay Kumaar
  • 690
  • 7
  • 17
1

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)
dnoeth
  • 59,503
  • 4
  • 39
  • 56