SELECT * FROM tablename WHERE colname != 'contents'
现在与我的预期相反,这似乎与其内容为“NulL”的colname不匹配.看看how SQLite handles NULLs并且特别是这个条件“null OR true”对于sqllite是正确的我认为我的查询足以为colname选择NulL为NulL的行.我必须错误地理解这一点.任何人都可以为我阐明这一点吗?我可以用
SELECT * FROM tablename WHERE (colname != 'contents' OR colname IS NulL)
但我不认为我必须这样做.
谢谢
解决方法 您可以使用IS NOT而不是!=来解决此问题.如果你看一下 the operators section of this document,你会看到:The IS and IS NOT operators work like = and != except when one or both of the operands are NulL. In this case,if both operands are NulL,then the IS operator evaluates to 1 (true) and the IS NOT operator evaluates to 0 (false). If one operand is NulL and the other is not,then the IS operator evaluates to 0 (false) and the IS NOT operator is 1 (true). It is not possible for an IS or IS NOT Expression to evaluate to NulL. Operators IS and IS NOT have the same precedence as =.
我自己测试了这个:
sqlite> create table temp( dataColumn,nullColumn );sqlite> insert into temp( dataColumn,nullColumn ) values ( 'test',NulL );sqlite> select * from temp where nullColumn != 'test';sqlite> select * from temp where nullColumn IS NOT 'test';test|sqlite> select * from temp where dataColumn IS NOT 'test';sqlite>
从更多的玩法来看,当你使用它来比较NulL的东西时,似乎!=运算符将评估为NulL:
sqlite> select ( 'contents' != NulL );sqlite> select ( 'contents' == NulL );sqlite> select ( 'contents' IS NulL );0sqlite> select ( 'contents' IS NOT NulL );1
大概这就是为什么你没有得到你期望的行为 – 你实际上是在说WHERE NulL!=’contents’,它的计算结果为NulL,并且不满足WHERE子句
总结以上是内存溢出为你收集整理的SQLite处理NULL全部内容,希望文章能够帮你解决SQLite处理NULL所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)