我无法从此查询中选择正确的值:
SELECT * FROM discounts
WHERE DiscLimit > 0 AND DiscLimit >= DiscRedeemed
OR DiscExp IS NOT NULL AND DiscExp <= NOW()
DiscExp
是DATETIME
字段,默认为NULL
。
我正在尝试从数据库中选择当前未到期的折扣:
SELECT * FROM discounts WHERE
:
似乎正在检索DATETIME
字段为NULL
的值。任何帮助将不胜感激。
答案 0 :(得分:1)
你需要括号!试试这个:
SELECT * FROM discounts
WHERE (DiscLimit > 0 AND DiscLimit >= DiscRedeemed)
OR (DiscExp IS NOT NULL AND DiscExp <= NOW())
但是,从您的问题来看,我不确定OR
是否正确。如果您希望它满足以下所有条件:
你想要,这个:
SELECT * FROM discounts
WHERE DiscLimit > 0 AND DiscLimit >= DiscRedeemed
AND DiscExp IS NOT NULL AND DiscExp <= NOW()
或者,如果您希望它满足以下所有条件:
你想要这个:
SELECT * FROM discounts
WHERE DiscLimit > 0 AND DiscLimit >= DiscRedeemed
AND (DiscExp IS NULL OR DiscExp <= NOW())
请注意,您实际上不需要使用DiscExp IS NOT NULL AND DiscExp <= NOW()
,因为NULL
不会评估为<= anything
。
希望这有帮助!
答案 1 :(得分:0)
您的逻辑问题是括号。但是,你可以写:
SELECT d.*
FROM discounts d
WHERE DiscLimit > 0 AND
DiscLimit >= DiscRedeemed AND
DiscExp <= NOW();
NULL
比较是不必要的,因为当一个或两个操作数都是NULL
时,任何比较运算符都返回not-true。