Working SQL Fiddle
排序以天數從當前日期的日期的diff的絕對值?如果需要替換當前日期由用戶選擇的日期
Create table foo(
ID int,
eventDate date);
Insert into foo (ID, eventDate)
values (1,'2017-01-01'),
(2,'2017-01-02'),
(3,'2017-01-03'),
(4,'2017-01-04'),
(5,'2017-01-05'),
(6,'2017-01-06'),
(7,'2017-01-07'),
(8,'2017-01-08'),
(9,'2017-01-09');
SELECT *
FROM foo
ORDER BY abs(dateDiff(eventDate,'2017-01-05')), EventDate
返回:
ID eventDate
5 January, 05 2017 00:00:00
4 January, 04 2017 00:00:00
6 January, 06 2017 00:00:00
3 January, 03 2017 00:00:00
7 January, 07 2017 00:00:00
2 January, 02 2017 00:00:00
8 January, 08 2017 00:00:00
1 January, 01 2017 00:00:00
9 January, 09 2017 00:00:00
如果兩個日期是由相同的區別分開。哪個應該先到? – xQbert