MySQL:使用单个查询在整个表中将'NULL'替换为实际NULL值的字符串

问题描述 投票:0回答:1

当我尝试查询时:

SELECT * 
FROM my_table
where my_column is Null 

它返回0个结果。该列是整数。

但是当我这样做时:

SELECT * 
FROM my_table
where my_column = 'Null' 

它返回预期结果。有趣的是,返回的行的值为0。

在我做之前

update my_table set my_column = Null where my_column = '0';

它曾经返回'0'。

这可能是什么原因,以及使它们为NULL而不是'NULL'或'0'或0的可能解决方案。

这让我发疯,我花了4个多小时来解决这个问题。我的表到处都有这些无意义的值。因此,如果有某种方法可以解决我的表而不是单列的问题,那就更好了。

这里是NULL图片here is a picture with is NULL

这里是带有'NULL'的图片here is a picture with = 'NULL'

这是一张图片,其中NULL在其他列中按预期工作。

enter image description here

mysql string null data-cleaning data-processing
1个回答
0
投票

您说my_column列是整数,因此该列不可能包含值'NULL'。当您应用此条件时:

where my_column = 'Null' 

字符串文字'Null'被隐式转换为整数,由于此转换无法成功,结果为0,您的条件等效于:]

where my_column = 0  

您可以像这样更新表格:

update tablename
set my_column = null
where my_column = 0
© www.soinside.com 2019 - 2024. All rights reserved.