sql datediff问题

发布于 2024-11-07 12:55:03 字数 536 浏览 0 评论 0原文

我正在尝试计算两个日期(当前日期和我的会员在网站上订阅的日期)之间的差异。如果差异超过 30 天,我就会关闭他们的订阅。我就是无法让它发挥作用。

$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);
}

I'm trying to calculate the difference 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天白云 2024-11-14 12:55:03

为什么不发表一份声明?

UPDATE monthlydues
SET `active`='N'
WHERE DATEDIFF(curdate(), date) > 30

那么这个?

$strsql="UPDATE monthlydues SET `active`='N' WHERE DATEDIFF(curdate(), `date`) > 30";

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";
这样的小城市 2024-11-14 12:55:03

您必须指定您想要什么样的差异。

例如,对于 SQL Server,您可以像这样指定天数差异:

DATEDIFF(day, date, curdate())

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())
花开半夏魅人心 2024-11-14 12:55:03

以下内容可能会对您有所帮助,但这具体取决于您想要做什么;此查询将对所有成员进行操作,而不仅仅是由指定 id 标识的成员:

UPDATE monthlydues SET active = 'N' 
WHERE DATEDIFF(NOW(), `date`) > 30;

如果您希望针对特定成员,只需添加一个约束:

UPDATE monthlydues SET active = 'N'
WHERE memid = :memberid 
AND DATEDIFF(NOW(), `date`) > 30;

编辑: NOW() 是一个 MySQL 函数。在差异引擎上可能会有所不同

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文