DNB5brims
DNB5brims

Reputation: 30618

Can i make my customised sql order by function?

I am using MySQL, and I check some SQL tutorial, I see that that is a statement that called ORDER BY, which is usually ORDER BY a column, with AESC or DESC. But is this possible to ORDER the data by different sorting algo?

For example, I have follow value in my DB:

`id`,`name`,`notice_day`

I would like to sort the day with today first, and than the day that near today, and then, tomorrow....bababa, after finish sorting the day from today, and future, yesterday, and so far....Here is the example, assame today is 1/1/2012

`1`, `peter`, `1/1/2012`
`2`, `tom`, `31/12/2011`
`3`, `mary`, `1/2/2012`
`4`, `steve`, `1/1/2011`
`5`, `bill`, `1/4/2012`

The sorting order I would like to is here:

`1`, `peter`, `1/1/2012`

`3`, `mary`, `1/2/2012`

`5`, `bill`, `1/4/2012`

`2`, `tom`, `31/12/2011`

`4`, `steve`, `1/1/2011`

Upvotes: 1

Views: 571

Answers (2)

mikeb
mikeb

Reputation: 366

so ascending if notice_day is today or in the future, descending if the notice_day is in the past? could do something like:

select * from my_table order by if(notice_day >= date(now()), notice_day, 9999) asc, notice_day desc

Upvotes: 1

Alexander Wigmore
Alexander Wigmore

Reputation: 3186

SELECT * FROM ##### ORDER BY CONVERT(DateTime, EventDate,101)  DESC

mySQL isn't my strong point, but I believe the above should work.

Upvotes: 0

Related Questions