Home > OS >  How to get start and end date where today's current time is in?
How to get start and end date where today's current time is in?

Time:04-24

I have only one column called "date". And in that column I have the following data:

2022-01-07
2022-01-22
2022-02-08
2022-02-20 
2022-03-02
2022-03-15
2022-03-25
2022-04-04
2022-04-20 select date
2022-05-05 select date
2022-05-17
2022-05-29
2022-06-15
2022-06-24
2022-07-04
2022-07-13
2022-07-22
2022-07-31
2022-08-10
2022-08-27
2022-09-12
2022-09-28
2022-10-10
2022-10-26
2022-11-10
2022-11-25
2022-12-07
2022-12-20
2022-12-30
2023-01-14
2023-01-28
2023-02-11
2023-02-22
2023-03-06
2023-03-15
2023-03-24
2023-04-05
2023-04-17
2023-04-29
2023-05-08

I needed to know which moment of this date I am. For example, if today is day 2022-04-24 then i need to select before and after date.

I need to select the two dates according to the current time where I am.

How can I do this in MySQL?

CodePudding user response:

You could do this via two scalar limit subqueries:

SELECT
    (SELECT date FROM yourTable WHERE date <= CURDATE() ORDER BY date DESC LIMIT 1) AS start,
    (SELECT date FROM yourTable WHERE date >= CURDATE() ORDER BY date LIMIT 1) AS end;
  • Related