calculating date in update mysql

Asked

Viewed 66 times

1

Good need to update my accounts that are past due more than 5 months, I wanted to do this directly in mysql. The update is like this:

UPDATE contas
SET baixa_valivade = true
WHERE data_vencimento (Preciso verificar se já esta vencida a mais de 5 meses)

1 answer

3


Use the DATE_ADD():

UPDATE contas
SET baixa_valivade = true
WHERE data_vencimento BETWEEN DATE_ADD(CURRENT_DATE(), INTERVAL -5 MONTH) AND CURRENT_DATE()

If the need is to update items with delay date greater than 5 months, the code would be as follows:

UPDATE contas
SET baixa_valivade = true
WHERE data_vencimento < DATE_ADD(CURRENT_DATE(), INTERVAL -5 MONTH)
  • Good tested here and did not work very well, I have 2 accounts. 2017-09-26 and 2018-07-26. Should update the account 2017-09-26, but this updating the account 2018-07-26.

  • by today’s date, 5 months back would catch the 07/18 (-2 months).. a 09/12 is -12 months.. did not understand the error =p

  • Well I guess I didn’t know how to explain it right, the update should be given in the accounts that are more than 5 months late. Account 09/17 is more than 5 months late and account 07/18 has not yet completed 5 months.

  • cashed.. updated the answer, see if it solves!

  • 1

    Okay, it worked. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.