在SQL Server中,如果 nullParam=NULL
在where子句中,它总是计算为false.这是违反直觉的,并且给我带来了许多错误.我确实理解IS NULL
和IS NOT NULL
关键字是正确的方法.但是为什么SQL服务器会以这种方式运行?
有什么区别
where x is null
Run Code Online (Sandbox Code Playgroud)
和
where x = null
Run Code Online (Sandbox Code Playgroud)
为什么后者不起作用?
似乎在PostgreSQL中,empty_field != 1
(或其他一些值)是假的.如果这是真的,有人可以告诉我如何与空字段进行比较吗?
我有以下查询,转换为"选择用户组中尚未投票的所有帖子:
SELECT p.id, p.body, p.author_id, p.created_at
FROM posts p
LEFT OUTER JOIN votes v ON v.post_id = p.id
WHERE p.group_id = 1
AND v.user_id != 1
Run Code Online (Sandbox Code Playgroud)
即使投票表为空,它也不输出任何内容.也许我的查询有问题,而不是上面的逻辑?
编辑:似乎改变v.user_id != 1
了v.user_id IS DISTINCT FROM 1
,完成了这项工作.来自PostgreSQL文档:
对于非空输入,IS DISTINCT FROM与<>运算符相同.但是,当两个输入都为null时,它将返回false,当只有一个输入为null时,它将返回true.
你能帮我吗,我需要了解两者之间的区别
select * from table where field <> NULL;
Run Code Online (Sandbox Code Playgroud)
和
select * from table where field is not NULL;
Run Code Online (Sandbox Code Playgroud)
看看
SELECT COUNT(*) where (1 = null) -- return 0
SELECT COUNT(*) where (1 <> null) -- return 0
SELECT COUNT(*) where (1 is not null) -- return 1
SELECT COUNT(*) where (null = null) -- return 0
SELECT COUNT(*) where (null <> null) -- return 0
SELECT COUNT(*) where (null is null) -- return 1
SELECT COUNT(*) where (null is …
Run Code Online (Sandbox Code Playgroud) 我有一行满1,2,3和null.为什么没有两个返回相同的结果:
select * from foo where foobar not in (1, 2, 3);
select * from foo where foobar is not null;
Run Code Online (Sandbox Code Playgroud)
第一个返回空集,而第二个返回广告.现在我有点困惑:-D在任何地方都有某种"新手SQL中的NULL"文档吗?:-D
(我正在使用Oracle,如果这很重要)
我有2张桌子A和B.
表A.
subgroup | maingroup |
------------------------------------------
NULL | A |
NULL | A |
Top | B |
Top | B |
Run Code Online (Sandbox Code Playgroud)
表B.
subgroup
---------------
top
NULL
Run Code Online (Sandbox Code Playgroud)
我正在运行此查询.
select * from a
join b
on a.subgroup=b.subgroup
group by a.subgroup,a.maingroup,b.subgroup
Run Code Online (Sandbox Code Playgroud)
我得到了这个输出
subgroup | maingroup | subgroup
-------------------------------------------------------
Top | B | Top
Run Code Online (Sandbox Code Playgroud)
我担心的是为什么NULL不匹配,并给我输出像NULL A Null.
我正在使用MSSQL
考虑以下代码 - 有人可以向我解释为什么一个查询不会抛出错误?我可以理解为什么它没有返回记录,但是它想要做什么?请记住,colA是一个int.它在2000,2005和2008 r2上的工作原理相同
create table #foo
( colA int )
insert into #foo
(colA)
values
(null)
select * from #foo --returns the one record we just created
select * from #foo where colA = null --does not throw an error and does not return a record! why??
select * from #foo where colA is null --returns the record
drop table #foo
Run Code Online (Sandbox Code Playgroud)
这个表中是否存在可以返回colA = null的记录?
我目前没有任何其他数据库供我试用 - 这是标准还是特定于MSSQL的行为?
我解雇了一个查询
SELECT * FROM tblEmp WHERE emp_id = 9737 AND mgr_id = NULL
Run Code Online (Sandbox Code Playgroud)
它没有回报任何价值.直到昨天它才这样做.什么可能改变?我们很多人在同一台服务器上工作,所以有可能有人改变了一些东西.
为了获得结果,我必须解雇
SELECT * FROM tblEmp WHERE emp_id = 9737 AND mgr_id IS NULL
Run Code Online (Sandbox Code Playgroud)
这是给出适当的输出.
由于我正在使用此查询,.Net
我担心如果设置重置为上一个时它会中断.会吗?
sql ×7
sql-server ×5
null ×4
compare ×1
database ×1
join ×1
mysql ×1
oracle ×1
postgresql ×1
select ×1