简体   繁体   中英

MySql using interval with a list of dates

I'm willing to fetch all the dates that are listed in the list reducing 1 day I'm trying something like that:

select date
from MyTable
where date in ('2022-03-22', '2022-03-18', '2022-03-11') - interval 1 day

I wish to receive:

'2022-03-21', '2022-03-17', '2022-03-10'

It can be done with intervals.

Here is a link to how it works: https://www.db-fiddle.com/f/3PnzHErrf2fZFGZY67K12X/48

SELECT ADDDATE("2022-03-22", INTERVAL -1 DAY);
SELECT ADDDATE("2022-03-18", INTERVAL -1 DAY);
SELECT ADDDATE("2022-03-11", INTERVAL -1 DAY);

You can also do it with a concat for example: https://www.db-fiddle.com/f/3PnzHErrf2fZFGZY67K12X/49

SELECT CONCAT(ADDDATE("2022-03-22", INTERVAL -1 DAY),' | ',
              ADDDATE("2022-03-18", INTERVAL -1 DAY),' | ',
              ADDDATE("2022-03-11", INTERVAL -1 DAY));

You can add 1 day to the date column's value to compare it against the IN list:

SELECT date
FROM MyTable
WHERE date + INTERVAL 1 DAY IN ('2022-03-22', '2022-03-18', '2022-03-11');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM