【发布时间】:2016-12-01 20:09:36
【问题描述】:
使用 Sybase ASE 15.7(但我认为 MSSqlServer 也是如此), 我刚遇到这种奇怪的行为:
假设我有一个非常简单的 2 列表格:
create table testtable(
id int identity not null,
name varchar(50) null
)
在其中,我将插入 2 个不同的值和 null 几次:
insert into testtable( name ) select "VAL1"
insert into testtable( name ) select "VAL1"
insert into testtable( name ) select "VAL1"
insert into testtable( name ) select "VAL1"
insert into testtable( name ) select "VAL2"
insert into testtable( name ) select "VAL2"
insert into testtable( name ) select null
insert into testtable( name ) select null
所以这是我表的内容:
select * from testtable
我得到:
1 VAL1
2 VAL1
3 VAL1
4 VAL1
5 VAL2
6 VAL2
7 NULL
8 NULL
现在奇怪的事情开始了,假设我想要除“VAL1”之外的所有值,我会发出这个查询:
select * from testtable where name <> "VAL1"
我当然有 2 条 VAL2 行,但没有那些为 null 的。好像 null 和 VAL1 相等
5 VAL2
6 VAL2
当然,我知道我应该发出这个查询:
select * from testtable where isnull( name, "" ) <> "VAL1"
我在这里发帖的原因不是我想为此找到解决方法,(我非常了解它并从一年开始就使用它),更多的是更好地理解 sybase “思考”空值的方式如果有人可以清楚地解释基本逻辑,因为多年来我解决了它,但我仍然不明白这一点。
感谢和问候
【问题讨论】:
-
你能检查我的答案吗
-
好的,我明白了。因为它无法与其他值进行比较,所以如果我在该列上发出搜索条件,则不会检索它...
标签: sql-server tsql null sybase isnull