0

I have the following table named 'product'

product_id (BIGINT) | product_name (VARCHAR) | expiration_date (DATE) | entered_date (DATETIME) |
 1234            | ABC                    |     NULL               |    2013-07-24 13:17:32  |
 6789            | ABC                    |     NULL               |    2013-07-19 12:04:12  |  
 1234            | ABC                    |     NULL               |    2013-07-05 13:34:16  |  
 2343            | ABC                    |     NULL               |    2013-06-04 17:16:51  |  

I would like to update the expiration_date entries based on the eneterd_date column. If entered_date + 30 days > NOW(), then set the expiration_date = 30 days ahead else expiration_date = 60 days ahead

product_id (BIGINT) | product_name (VARCHAR) | expiration_date (DATE)  | entered_date (DATETIME) |
 1234               | ABC                    |  2013-08-24             |    2013-07-24 13:17:32  |
 6789               | ABC                    |  2013-08-19             |    2013-07-19 12:04:12  |  
 1234               | ABC                    |  2013-08-05             |    2013-06-05 13:34:16  |  
 2343               | ABC                    |  2013-08-24             |    2013-06-04 17:16:51  |  

I am trying my hands on ADDDATE() and other date functions in MySql but dint't have any good luck so far.

Please let me know you could help me out in getting the following result.

enter code here
4

2 回答 2

1

您能否指定 ADDDATE() 函数的问题?

我尝试了这样的事情,它奏效了。

CREATE TABLE dates
    (
     id int auto_increment primary key, 
     date DATETIME DEFAULT NULL
    );

INSERT INTO dates
(date)
VALUES
(NOW());
UPDATE dates SET date = DATE_ADD(date,INTERVAL 10 DAY);

也许您的更新语句中的 where 子句是错误的。

你可以这样做

 WHERE DATE_ADD(date ,INTERVAL 30 DAY) > NOW()
于 2013-07-24T20:32:39.227 回答
1

试试这个(未经测试!)

 update product set expiration_date = (
      select if(dateadd(entered_date, interval 30 day) > now(),
          dateadd(entered_date,interval 30 day), 
          dateadd(entered_date,interval 60 day)))
于 2013-07-24T20:35:51.917 回答