简体   繁体   中英

Setting subscription dates in MySQL

I'm dealing with a website where people can subscribe to certain things for virtual money. I need to put the dates at which subscriptions end in the database. My table has a field "expiration" for that, which is a DATE.

When the user extends his subscription, I need to add 1 month to this date. However, if the subscription has already expired, I want to set "expiration" to 1 month from now, not to 1 month from when the subscription expired.

I've 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 have to use some SELECT queries beforehand?

MIN()MAX()函数用于分组:您需要LEAST()GREATEST()

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