Mysql: How to get the next date of a selected day?

My problem is that I have a form called "weekly setup" in which I select one or two days as weekly. If I have a weekly exit on Saturday, I want to find out the first date of Satur which comes in the first week of the selected month. So can someone tell me a mysql query for this problem.

Thanks in advance.

+4
source share
1 answer

It’s a little incomprehensible from your statement, but are you trying to find the next Saturday?

select date_add(now(), interval 7-dayofweek(now()) day); 

Which, unfortunately, will return today if you are on Saturday, so the sequence will be as follows:

 SET @OFFSET = 7-dayofweek(now()); SET @OFFSET = IF(@OFFSET = 0, 7, @OFFSET); select date_add(now(), interval @OFFSET day); 

which can be combined into one:

 select date_add(now(), interval IF(7-dayofweek(now()) = 0, 7, 7-dayofweek(now())) day) as next; 
+4
source

Source: https://habr.com/ru/post/1433206/


All Articles