简体   繁体   English

在MySQL中设置订阅日期

[英]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. 当用户延长订阅时,我需要在此日期添加1个月。 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. 但是,如果订阅已经过期,我想将“到期”设置为从现在开始的1个月,而不是从订阅到期后的1个月。

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? 有没有办法在1个查询中执行此操作,或者我是否必须事先使用一些SELECT查询?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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