Mysql update filtering by due date with tolerance of x Days

Asked

Viewed 158 times

1

I need to update only records that are expired and add a tolerance of x days Example
Tolerance of 5 days Today is = 11/10/2016

id= 1 maturity = 10/10/2016 // only update if today was day 15

id= 2 maturity = 06/10/2016 // perform update

id= 3 maturity = 10/10/2016 // only update if today was day 15

id= 4 maturity = 10/10/2016 // only update if today was day 15

Here’s my code but it’s not working right to rule

$query = mysql_query("UPDATE finan SET status='Vencido' WHERE status = 'Em Aberto' AND vencimento < CURDATE() + INTERVAL '$tolerencia' DAY ") or die(mysql_error());

2 answers

1

Try this way, assuming the due column is of type "date" or "datetime"

$query = mysql_query("UPDATE finan SET status='Vencido' WHERE status = 'Em Aberto' AND vencimento + INTERVAL '$tolerencia' DAY < NOW()") or die(mysql_error());

1


In your example you are putting the tolerance on the current date and not the due date. The update would thus be on the correct date:

UPDATE finan 
   SET status = 'Vencido' 
 WHERE status = 'Em Aberto' 
   AND CURDATE() > vencimento + INTERVAL'$tolerencia' day

Browser other questions tagged

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