簡體   English   中英

檢查DATETIME是否為14天前

[英]Checking to see if a DATETIME was 14 days ago

我要嘗試的是查詢數據庫中的表,以便他們激活帳戶14天后可以發送歡迎電子郵件...

成功發送電子郵件后,我將設置標志= 0,以便以后不再發送電子郵件。

我不知道如何將activation_date DATETIME與14天期限進行比較...

您可以使用datediff:

如果激活的日期差​​異= 14,則選擇行:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

WHERE 14 = DATEDIFF(activation_date, $today) 

您可以在查詢中將您的Activation_date與DATE_SUB(NOW(),INTERVAL 14天)進行比較。

使用datediff()函數,以下是查看其工作方式的示例: http ://sqlfiddle.com/#!2/a2581/7851

Select '2013-04-10', 

case 
when datediff( now() , '2013-04-10' ) = 14 
then 'Send eMail' 
else 'Don''t send eMail' 
end 

as SendeMail

UNION ALL 


Select '2013-04-11', 

case 
when datediff( now() , '2013-04-11' ) = 14 
then 'Send eMail' 
else 'Don''t send eMail' 
end 

as SendeMail

UNION ALL


Select '2013-04-15' , 

case 
when datediff( now() , '2013-04-15' ) = 14 
then 'Send eMail' 
else 'Don''t send eMail' 
end 

as SendeMail

當然,您可以在WHERE語句中使用它...

WHERE datediff( now() , '2013-04-11' ) = 14 

使用now()函數獲取當前日期。 我的示例日期(2013-04-11)應該替換為您的activation_date列。

您可以在where子句中使用date_sub函數

where activation_date > date_sub(now() ,interval 14 day) and flag <> 0

這里的示例代碼

SELECT * FROM my_table WHERE DATEDIFF( NOW() , activation_date ) = 14 AND active = 1 AND msg_welcome = 0

這是我需要的查詢! 非常感謝大家...只需要稍微修改一下您的建議即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM