In mysql I have a table with a date field
I would like to find the rows closest to a date gived, but prioritizing the row in the past.
In other word, if I give the date 2014-06-01
And in my data, I have 3 rows: 2014-05-01, 2014-06-02, 2014-05-20
I would like to output 2014-05-20
But if I don't have anything before that date, I woule like to output the first one
For the same date gived: 2014-06-01
If I have thoses rows: 2014-07-02, 2014-08-02, 2014-08-20
I would like to output 2014-07-02
The only way I think of doing this is using some union, 2 select (one past, one future), then a other select to limit it to one result...
I kinda find this over the top for a such simple request, and since this request will be called a lots, I would like to optimize if as much as I can
How would you do this?