我在一个MySQL查询中有一个CASE表达式,用于检查两个日期中的较小日期或较早日期,但是我无法使其正常工作
update SaveContactForm7_1
SET `TStat` =
CASE
WHEN
(`DateP1` IS NOT NULL OR `DateP2` IS NOT NULL)
AND `T1A` BETWEEN (`Date`-1 AND (`DateP1` OR `DateP2`))
AND `T1A` < `SLH`
AND `T2A` IS NULL
AND `T3A` IS NULL
THEN 'T1'
WHEN
(`DateP1` IS NOT NULL OR `DateP2` IS NOT NULL)
AND `T2A` BETWEEN (`Date`-1 AND (`DateP1` OR `DateP2`))
AND `T2A` < `SLH`
AND `T3A` IS NULL
THEN 'T2'
END;
总是求值为T1或T2或完全跳过T1A
<SLH
求值。我曾尝试将花括号(T1A
<SLH
放在括号中,但这也不起作用。
select * from SaveContactForm7_1 where `T1A`>`SLH` and TStat='T1';
,它显示“转义”更新查询的所有行。 DateP1
,DateP2
,Date
,T1A
,T2A
...都是日期类型。如何编写更新查询,以便评估T1A
<SLH
。你能请教吗?
答案 0 :(得分:1)
这不会达到您的期望:
AND `T1A` BETWEEN (`Date`-1 AND (`DateP1` OR `DateP2`))
我想您要这样:
AND `T1A` BETWEEN (`Date`-1 AND COALESCE(`DateP1` `DateP2`))