简体   繁体   English

MySQL:用单个查询在整个表中用实际 NULL 值替换“NULL”作为字符串

[英]MySQL: Replacing 'NULL' as string with actual NULL values in whole table with single query

When I try to query:当我尝试查询时:

SELECT * 
FROM my_table
where my_column is Null 

it returns 0 results.它返回 0 个结果。 The column is integer.该列是 integer。

But when I do:但是当我这样做时:

SELECT * 
FROM my_table
where my_column = 'Null' 

it returns expected results.它返回预期的结果。 And interestingly returned rows has 0 value.有趣的是,返回的行的值为 0。

And before I did在我做之前

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

it used to return '0'.它曾经返回“0”。

What could be the reason of this, and what would be the possible solution to have them NULL instead of 'NULL' or '0' or 0.这可能是什么原因,以及让它们 NULL 而不是“NULL”或“0”或 0 的可能解决方案是什么。

This is driving me crazy and I spend more then 4 hours trying to fix this.这让我发疯,我花了超过 4 个小时试图解决这个问题。 My table has these non-sense values all around.我的桌子到处都是这些无意义的值。 So if any method to fix this for my table instead of single column that would be better.因此,如果有任何方法可以为我的表而不是单列解决这个问题,那就更好了。

here is a picture with is NULL这是一张图片 NULL 这是一张为NULL的图片

here is a picture with 'NULL'这是一张带有“NULL”的图片这是一张带有 = 'NULL' 的图片

and here is a picture with is NULL working as expected in different column.这是一张图片,NULL 在不同的列中按预期工作。

在此处输入图像描述

You say that the column my_column 's data type is INTEGER , so it is impossible that the column contains the value 'NULL' .您说列my_column的数据类型是INTEGER ,因此该列不可能包含值'NULL'
When you apply this condition:当您应用此条件时:

where my_column = 'Null' 

the string literal 'Null' is implicitly converted to an integer and since this conversion can not succeed the result is 0 and your condition is equivalent to:字符串文字'Null'被隐式转换为 integer 并且由于此转换无法成功,因此结果为0 ,您的条件相当于:

where my_column = 0  

You can update your table like this:你可以像这样更新你的表:

update tablename
set my_column = null
where my_column = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM