为什么相等的日期出现在 select 中,而 where 语句说它们不应该匹配?
Why does equal dates appear in select when the where statement says they should not match?
我正在做一些数据侦察,想看看两个日期字段中的数据是否匹配。每列都来自不同的 table,因此我想检查它们是否相同。由于它们来自不同的 table 并且我们没有日期同步功能,所以两个字段会相差几分之一秒,也许到处都是一秒。我不关心时间,我只关心这两个字段是否反映相同的日期,因此在我的 where 子句中我将它们转换为日期......但是,这似乎不起作用,因为我仍然获取所有 281 条记录,有或没有
and cast(write_off_date as date) <> cast(write_off_action_date as date)
我的查询:
select status
,write_off_date
,write_off_action_date
from renamed
where write_off_date is not null or write_off_action_date is not null
and cast(write_off_date as date) <> cast(write_off_action_date as date)
数据输出样本是:
把OR
条件放在括号里:
where (write_off_date is not null or write_off_action_date is not null)
and cast(write_off_date as date) <> cast(write_off_action_date as date)
基本原理 - 您的代码如下:
condition1 or condition2 and condition3
and
的逻辑优先级高于or
,所以这等价于:
condition1 or (condition2 and condition3)
...这不是你想要的。
也就是说,整个 where
子句毫无意义。如果任何一个日期是null
,不等式条件无论如何都不能满足,所以not null
条件是不必要的。
我正在做一些数据侦察,想看看两个日期字段中的数据是否匹配。每列都来自不同的 table,因此我想检查它们是否相同。由于它们来自不同的 table 并且我们没有日期同步功能,所以两个字段会相差几分之一秒,也许到处都是一秒。我不关心时间,我只关心这两个字段是否反映相同的日期,因此在我的 where 子句中我将它们转换为日期......但是,这似乎不起作用,因为我仍然获取所有 281 条记录,有或没有
and cast(write_off_date as date) <> cast(write_off_action_date as date)
我的查询:
select status
,write_off_date
,write_off_action_date
from renamed
where write_off_date is not null or write_off_action_date is not null
and cast(write_off_date as date) <> cast(write_off_action_date as date)
数据输出样本是:
把OR
条件放在括号里:
where (write_off_date is not null or write_off_action_date is not null)
and cast(write_off_date as date) <> cast(write_off_action_date as date)
基本原理 - 您的代码如下:
condition1 or condition2 and condition3
and
的逻辑优先级高于or
,所以这等价于:
condition1 or (condition2 and condition3)
...这不是你想要的。
也就是说,整个 where
子句毫无意义。如果任何一个日期是null
,不等式条件无论如何都不能满足,所以not null
条件是不必要的。