【问题标题】:NUMBER(10,2) not equal to check in OracleNUMBER(10,2) 不等于在 Oracle 中检查
【发布时间】:2015-11-16 11:08:15
【问题描述】:

我在 Table(Oracle) 中有一个 Number 字段,如下所示

TEST_HOURS NUMBER(10,2)

当我尝试以下 SQL 时它不起作用

select t.test_hours from test_hours t where
where (t.test_hours != 1 or t.test_hours is not null)
and t.id = 11

当我删除此(t.test_hours != 1 or t.test_hours != null) 时,它工作正常,但当我包含上述行时,它不会获取数据并且没有error

有人可以帮我解决这个问题吗?

谢谢

【问题讨论】:

  • 这种情况似乎毫无意义t.test_hours != 1 or t.test_hours != null。你想得到什么?
  • 您无法与null 进行比较。始终使用is nullis not null
  • @vkp 尝试不为空,它没有帮助
  • @EgorSkriptunoff 我正在尝试检查 test_hours 的值是否为 1,如果它的值为 1,则如果不获取该行,则不获取该行,我将传递我给出的测试值动态有价值但没有帮助

标签: sql database oracle numbers conditional-statements


【解决方案1】:

我想你想要的是 and,而不是 or

select t.test_hours
from test_hours t
where (t.test_hours <> 1 and t.test_hours is not null) and t.id = 11

顺便说一句,与NULL 的比较是不必要的,因为它会失败。所以更简单的版本是:

select t.test_hours
from test_hours t
where t.test_hours <> 1 and t.id = 11

【讨论】:

    【解决方案2】:

    正如 Egor 的评论中所指出的,您在相关列中的情况毫无意义。此外,您应该使用IS NOT NULLIS NULL,而不是!=。当您使用!=,并且左侧的内容为空时,结果将等于NULL,而不是TRUEFALSE

    即使使用IS NOT NULL,您的条件也是无效的,因为对于test_hours 等于1,条件将始终为TRUE,因为:

    test_hours != 1 FALSE
    OR
    test_hours IS NOT NULL TRUE
    

    因此结果将为 TRUE。

    您可能想要使用不同的条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 2021-08-01
      • 1970-01-01
      • 2021-09-13
      • 2018-01-17
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多