如何处理 sql 中的 null 和 NULL 值
How to handle null and NULL value in sql
所以我在 mysql 中一直面临这种奇怪的情况,其中以某种方式在我的 table 中插入了空值。
我说的是 null 值而不是 NULL 值。
我附上了图片以便更好地理解
如您所见,名称列有 null,mobile_no 有 NULL
所以在使用这个查询之后
select Case when t1.name IS NULL then 'NA'
when t1.name= 'NA' or 'null' or NULL then 'NA'
else t1.name end as 'Name',
Case when t1.mobile_no IS NULL then 'NA'
when t1.mobile_no= 'NA' or 'null' or NULL then 'NA'
else t1.mobile_noend as 'Mobile no' from student;
在此之后我得到了这个结果
|Name|Mobile no|
----------------
|null|NA |
但我想要下面的结果
|Name|Mobile no|
----------------
|NA |NA |
可以试试用IN语句吗?那应该可以。
select Case WHEN (t1.name IN ('NA', 'null') OR t1.name IS NULL) THEN 'NA' else t1.name end as 'Name',
Case WHEN (t1.mobile IN ('NA', 'null') OR t1.mobile IS NULL) THEN 'NA' else t1.mobile_no end as 'Mobile no' from student;
要比较具有多个值的列,请使用 col IN (x, y, z)
,而不是 col = x OR y OR z
。
您也无法使用 =
或 IN
与 NULL
进行比较,因此必须单独检查。
select
Case
when t1.name IS NULL OR t1.name IN ('NA' or 'null') then 'NA'
else t1.name
end as 'Name'
所以我在 mysql 中一直面临这种奇怪的情况,其中以某种方式在我的 table 中插入了空值。 我说的是 null 值而不是 NULL 值。
我附上了图片以便更好地理解
如您所见,名称列有 null,mobile_no 有 NULL
所以在使用这个查询之后
select Case when t1.name IS NULL then 'NA'
when t1.name= 'NA' or 'null' or NULL then 'NA'
else t1.name end as 'Name',
Case when t1.mobile_no IS NULL then 'NA'
when t1.mobile_no= 'NA' or 'null' or NULL then 'NA'
else t1.mobile_noend as 'Mobile no' from student;
在此之后我得到了这个结果
|Name|Mobile no|
----------------
|null|NA |
但我想要下面的结果
|Name|Mobile no|
----------------
|NA |NA |
可以试试用IN语句吗?那应该可以。
select Case WHEN (t1.name IN ('NA', 'null') OR t1.name IS NULL) THEN 'NA' else t1.name end as 'Name',
Case WHEN (t1.mobile IN ('NA', 'null') OR t1.mobile IS NULL) THEN 'NA' else t1.mobile_no end as 'Mobile no' from student;
要比较具有多个值的列,请使用 col IN (x, y, z)
,而不是 col = x OR y OR z
。
您也无法使用 =
或 IN
与 NULL
进行比较,因此必须单独检查。
select
Case
when t1.name IS NULL OR t1.name IN ('NA' or 'null') then 'NA'
else t1.name
end as 'Name'