sql datediff problem
I'm trying to calculate the differenc开发者_如何学运维e between 2 dates, the current date and the date my members subscribed on the site. If the difference is more than 30 days, I turn off their subscription. I just can't get it to work.
$strFind="SELECT DATEDIFF(date, curdate()) AS total FROM `monthlydues` WHERE `memid`=\"$curmemid\" ORDER BY `id` DESC LIMIT 1";
$result=mysql_query($strFind) or die(mysql_error());
$row=mysql_fetch_array($result);
$gtime=$row['total'];
if($gtime>30){
$strsql="UPDATE monthlydues SET `active`='N' WHERE `memid`=\"$curmemid\"";
mysql_query($strsql,$connect) or die(mysql_error());
$chkrow5=mysql_affected_rows($connect);
}
Why not one statement?
UPDATE monthlydues
SET `active`='N'
WHERE DATEDIFF(curdate(), date) > 30
So this?
$strsql="UPDATE monthlydues SET `active`='N' WHERE DATEDIFF(curdate(), `date`) > 30";
You have to specify what kind of difference you want.
For SQL Server for example you specify a difference in days like this:
DATEDIFF(day, date, curdate())
The following may help you, though it depends on exactly what you want to do; This query will operate on all members, not just the member identified by a specified id:
UPDATE monthlydues SET active = 'N'
WHERE DATEDIFF(NOW(), `date`) > 30;
If you wish to target a specific member, simply add a constraint:
UPDATE monthlydues SET active = 'N'
WHERE memid = :memberid
AND DATEDIFF(NOW(), `date`) > 30;
EDIT: NOW() is a MySQL function. Is likely to be different on diff engines
精彩评论