如何在WHERE条件中包含NULL值 [英] How do I include NULL values into WHERE condition

查看:90
本文介绍了如何在WHERE条件中包含NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试使用Where条件中的列名检索所有值:我有使用此方法的原因(这个问题是为了帮助回答我的错误而不是我的实际代码):



Hi,

I am trying to retrieve all values using the column name in the Where condition: I have my reasons using this method (this question is to help answer my errors not my actual codes) :

SELECT *
FROM Table1
WHERE Column1 IN (Column1)





问题是在返回期间它不会在第1列中包含NULL值。我如何接受NULL值?



The problem is it does not take in NULL values in column 1 during return. How do I take in NULL values as well?

推荐答案

该查询没有意义,但是如果你想检查空值



That query makes no sense, however if you want to check for nulls

select fieldlist from table where somefield is null





或忽略空值





or to ignore nulls

select fieldlist from table where somefield is not null


这是您的要求的样本;请参考并在您的要求中实现相同的内容。



This is the sample for your requirement; refer this and implement the same in your requirement.

declare @tab table
(
	Id int,
	name varchar(100)
)

insert into @tab
values (1,'n'),(2,'d'),(3,null),(4,'g'),(5,null)

select * from @tab
where name in ('n','d') or name is null


SELECT *

FROM TableA

WHERE Col1 IN('a','b','c')



UNION ALL



SELECT *

来自TableA

WHERE Col1 IS NULL
SELECT *
FROM TableA
WHERE Col1 IN ('a','b','c')

UNION ALL

SELECT *
FROM TableA
WHERE Col1 IS NULL


这篇关于如何在WHERE条件中包含NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆