Setting a subscription date in MySQL

I am dealing with a website where people can subscribe to certain things for virtual money. I need to specify the end date of the subscription in the database. My table has an "expiration" field for this, which is a DATE.

When a user expands his subscription, I need to add 1 month to this date. However, if the subscription has already expired, I want to set the “expiration” to 1 month from now, and not 1 month from the date the subscription expires.

I tried:

UPDATE shop_user_rights SET expiration = ADDDATE(MAX(expiration, CURDATE()), INTERVAL 1 MONTH);

and

UPDATE shop_user_rights SET expiration = FROM_UNIXTIME(
 MIN(
  UNIX_TIMESTAMP(expiration),
  UNIX_TIMESTAMP(CURDATE())
 )
),
expiration = ADDDATE(expiration, INTERVAL 1 MONTH);

But both give syntax errors. Is there a way to do this in 1 query, or do I need to use some SELECT queries in advance?

+3
source share
1
+3

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


All Articles