'Return to default value after field equals zero

So I need help in trying to make a database field where it's default 7 and each day minuses 1 until its 0 then the day count starts again from 7. Is it possible? If so how?



Solution 1:[1]

SELECT 7 - ABS(0-DAYOFWEEK(NOW())) will work.

Of course, since there are only 7 days in a week, this will go 7,6,5,4,3,2,1,7. If you want it 0 indexed, use SELECT 6 - ABS(0-DAYOFWEEK(NOW())) to get 6,5,4,3,2,1,0,6.

I've interpreted your question somewhat liberally, however. If you actually want to count 8 days (7,6,5,4,3,2,1,0,7), you want SELECT 8 - (TO_DAYS(NOW())%8).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1