You can choose the nearest entry for a given date?

There is a table (with fields id, datetime, sum). Stored date in the format YYYY-m-d H:i:s
In this table many records.
We have the desired sum, we must choose the desired date.
Need to get one to the nearest value on the date (in both directions) containing the desired sum.

Is it possible to rotate by means of the database?
April 18th 20 at 12:46
2 answers
April 18th 20 at 12:48
SELECT * FROM my_table WHERE datetime < %s ORDER BY datetime DESC LIMIT 1
SELECT * FROM my_table WHERE datetime >= %s ORDER BY datetime ASC LIMIT 1

And then in page to compare which of two strings is closer.
April 18th 20 at 12:50
well, one option is to operate the difference of dates - for example to get the first record, sorted by ascending order of this Delta, more precisely its absolute value.

in mssql syntax it will look like

select top 1 ... from ... order by abs(datediff(sec,date1,date2))

Find more questions by tags MySQLSQL