SQL 选择列为空的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3369041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:01:46  来源:igfitidea点击:

Select rows where column is null

sql

提问by anjum

How do you write a SELECT statement that only returns rows where the value for a certain column is null?

如何编写仅返回某列值为空的行的 SELECT 语句?

回答by Jon Skeet

Do you mean something like:

你的意思是这样的:

SELECT COLUMN1, COLUMN2 FROM MY_TABLE WHERE COLUMN1 = 'Value' OR COLUMN1 IS NULL

?

?

回答by tdammers

I'm not sure if this answers your question, but using the IS NULL construct, you can test whether any given scalar expression is NULL:

我不确定这是否能回答您的问题,但使用 IS NULL 构造,您可以测试任何给定的标量表达式是否为 NULL:

SELECT * FROM customers WHERE first_name IS NULL

On MS SQL Server, the ISNULL() function returns the first argument if it's not NULL, otherwise it returns the second. You can effectively use this to make sure a query always yields a value instead of NULL, e.g.:

在 MS SQL Server 上,如果第一个参数不是 NULL,ISNULL() 函数返回第一个参数,否则返回第二个参数。您可以有效地使用它来确保查询始终产生一个值而不是 NULL,例如:

SELECT ISNULL(column1, 'No value found') FROM mytable WHERE column2 = 23

Other DBMSes have similar functionality available.

其他 DBMS 具有类似的可用功能。

If you want to know whether a column canbe null (i.e., is defined to be nullable), without querying for actual data, you should look into information_schema.

如果您想知道列是否可以为空(即,定义为可为空的),而不查询实际数据,您应该查看 information_schema。

回答by anishMarokey

Use Is Null

Is Null

select * from tblName where clmnName is null    

回答by hol

You want to know if the column is null

您想知道该列是否为空

select * from foo where bar is null

If you want to check for some value not equal to something and the column also contains null values you will not get the columns with null in it

如果您想检查某些不等于某值的值并且该列还包含空值,您将不会得到包含空值的列

does not work:

不起作用:

select * from foo where bar <> 'value'

does work:

确实有效:

select * from foo where bar <> 'value' or bar is null

in Oracle (don't know on other DBMS) some people use this

在 Oracle 中(在其他 DBMS 上不知道)有些人使用它

select * from foo where NVL(bar,'n/a') <> 'value'

if I read the answer from tdammers correctly then in MS SQL Server this is like that

如果我正确地从 tdammers 中读取了答案,那么在 MS SQL Server 中就是这样

select * from foo where ISNULL(bar,'n/a') <> 'value'

in my opinion it is a bit of a hack and the moment 'value' becomes a variable the statement tends to become buggy if the variable contains 'n/a'.

在我看来,这是一个小技巧,当“值”成为变量时,如果变量包含“n/a”,则语句往往会变得有问题。

回答by MAK

for some reasons IS NULL may not work with some column data type i was in need to get all the employees that their English full name is missing ,I've used :

由于某些原因,IS NULL 可能不适用于某些列数据类型,我需要让所有员工都没有他们的英文全名,我使用过:

**SELECT emp_id ,Full_Name_Ar,Full_Name_En from employees where Full_Name_En = ' ' or Full_Name_En is null **

**从 Full_Name_En = ' ' 或 Full_Name_En 为空的员工中选择 emp_id ,Full_Name_Ar,Full_Name_En **

回答by Taras Melnyk

select Column from Table where Column is null;

回答by Md.Aminul Islam

select * from tableName where columnName is null

select * from tableName 其中 columnName 为空