Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL表中查找空值的数量_Sql - Fatal编程技术网

如何在SQL表中查找空值的数量

如何在SQL表中查找空值的数量,sql,Sql,假设我有一个employee表。它有名字、薪水和年龄等列。如果我想检查名称中是否有空值。我得写信 SELECT name FROM EMPLOYEE WHERE name IS NULL; 但是我应该怎么做才能看到每列的空值数量呢?您可以使用下面的查询,但它取决于列的数量。它将给出每行的空值数 select ((case when column1 is null then 1 else 0 end) + (case when column2 is null then 1 else

假设我有一个employee表。它有名字、薪水和年龄等列。如果我想检查名称中是否有空值。我得写信

SELECT name FROM EMPLOYEE 
WHERE name IS NULL;

但是我应该怎么做才能看到每列的空值数量呢?

您可以使用下面的查询,但它取决于列的数量。它将给出每行的空值数

select 
  ((case when column1 is null then 1 else 0 end)
   + (case when column2 is null then 1 else 0 end)
   ----------------------------------------
   ----------------------------------------
   +(case when columnN is null then 1 else 0 end))
 from TableName 
为了你的案子

select 
 ((case when name is null then 1 else 0 end)
 +(case when salary is null then 1 else 0 end)
 +(case when age is null then 1 else 0 end))
 from employee

我假设您希望看到列的空值,您可以应用条件聚合

选择COUNT(当name为null时为1 end)作为name\u NullCount,
计数(如果薪资为空,则为1结束)作为薪资_nullCount,
计数(如果年龄为空,则为1结束)作为年龄
来自员工;

您可以从以下查询中找到每行的空值

SELECT 
(
 (CASE WHEN name IS NULL THEN 1 ELSE 0 END)+
  ...
  ...
  +(CASE WHEN colName IS NULL THEN 1 ELSE 0 END)
) AS sum_of_nulls
 FROM EMPLOYEE
 WHERE EmpId = 49

其中colName是数据库列名

最简单的方法可能是:

select 
    sum(case 
            when name is null then 1 
            when name is not null tehn 0 
        end) cname,
    sum(case 
            when salary is null then 1 
            when salary is not null then 0 
        end) csalary,
    ...
from employee
    limit 1;
select count(*) - count(name) as num_name_nulls,
       count(*) - count(col1) as num_col1_nulls,
       . . .
from employee;
但是,在这种情况下,我只需选择计数:

select count(*), count(name), count(col1), . . .
from employee;

然后我查看结果,看看计数是否不相等。

您使用的是哪种DBMS产品?“SQL”只是所有关系数据库使用的查询语言,而不是特定数据库产品的名称。请为您正在使用的数据库产品添加。请为codecount计数行添加说明。它不依赖于值0或1。总和加1+1+0+。。。因此,和的值对应于零值的数量。限制1显示一行值。如果未执行此操作,将显示员工表中具有相同总和值的所有行。