我的问题可能太愚蠢了,以至于我错过了一些非常明显的东西。我有一个表,其中包含代表 bool 值 1 或 0 的数字字段。我的表是这样创建的:
create table mytable ("summarized" NUMBER DEFAULT 0)
..我想查询数字字段“summarized”等于0的记录。现在看一下下面的查询,实际输出放在查询后面:
select count(*) from mytable where summarized = 0; -- 0
select count(*) from mytable where summarized = 1; -- 0
select count(*) from mytable where summarized != 0; -- 88223
select count(*) from mytable where summarized != 1; -- 1594
select count(*) from mytable where to_char(summarized) = '0'; -- 1594
select summarized, count(*) from mytable group by summarized; -- 1 = 88223, 0 = 1594
我的第一直觉是像第一个查询一样创建查询。我似乎无法理解为什么它不起作用,以及如果我想在 where 子句中使用“等于 0”,为什么我必须使用 to_char 函数。
编辑: 当我尝试答案时,我注意到我无法重现该问题,即使列名带有引号也是如此。复制我的表并删除旧表后,我的查询和应用程序再次按预期工作。我不确定是什么原因导致了异常,但现在已经消失了。
很抱歉占用了您的时间,但还是要感谢大家在答案中提出的有用的评论! (我希望我能为他们投一票,但我的声望仍然太低)
最佳答案
一个好的开始是对该列设置检查约束:
alter table mytable add constraint chk_mytable_summarized check (summarized in (0,1))
...还有一个非空约束...
alter table mytable modify (summarized not null)
(正如 Nagaraj 所说,不要在对象名称周围使用双引号)
关于sql - oracle db中number字段的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21257184/