MySQL使用之间查找记录

时间:2013-12-06 04:39:31

标签: mysql sql datetime select between

这是我的代码:

SELECT * 
FROM  `products`
WHERE
DATE_ADD( UTC_TIMESTAMP( ) , INTERVAL 7 HOUR ) 
BETWEEN DATE_SUB( STR_TO_DATE( IsPromotionDate,  '%m/%d/%Y %H:%i' ) , INTERVAL 3 DAY ) 
AND STR_TO_DATE( IsPromotionDate,  '%m/%d/%Y %H:%i' )

但结果会返回IsPromotionDate之前的所有产品,但不会返回IsPromotionDate之后的所有产品 - 3.我仍然没有弄清楚原因。

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT * FROM  `products`
WHERE DATE_ADD(UTC_TIMESTAMP( ), INTERVAL 7 HOUR) BETWEEN DATE_SUB(IsPromotionDate, INTERVAL 3 DAY) AND IsPromotionDate

SELECT * FROM  `products`
WHERE DATEDIFF(DATE_ADD(UTC_TIMESTAMP(), INTERVAL 7 HOUR), IsPromotionDate) BETWEEN 0 AND 3

答案 1 :(得分:1)

SELECT * 
FROM  `products`
WHERE
STR_TO_DATE( IsPromotionDate,  '%m/%d/%Y %H:%i')
BETWEEN NOW()
AND  DATE_SUB( now() , INTERVAL 3 DAY )