[英]How to replace values present in multiple columns?
I have a database table foo
and I want to replace null
values present in specific columns with empty string. 我有一个数据库表
foo
,我想用空字符串替换特定列中的null
值。 For removing null values present in particular column x
, I would do like 为了删除特定列
x
存在的空值,我想
update foo set x = '' where x is null;
Is there anyway I would do like, 反正有我想做的吗,
update foo set x,y,z = '' where x,y,z is null;
You can: 您可以:
set
x = case when x is null then '' else x,
y = case when y is null then '' else y,
z = case when z is null then '' else z
...
This will update all rows, where any of columns x , y or z has NULL value and uses COALESCE function to modify values only in columns where value is NULL: 这将更新所有行,其中x , y或z列中的任何一个具有NULL值,并使用COALESCE函数仅修改value为NULL的列中的值:
UPDATE
foo
SET
x = COALESCE(x, ''),
y = COALESCE(y, ''),
z = COALESCE(z, '')
WHERE
NULL IN (x, y, z)
Is simpler to do this: 这样做更简单:
UPDATE foo SET x='', y='', z='' WHERE x IS NULL AND y IS NULL AND z IS NULL
Or this one if you want to edit only the values that are NULL 如果只想编辑NULL值,则使用此选项
UPDATE foo SET x = IF(x IS NULL, '', x), y = IF(y IS NULL, '', y) ...
you should try this 你应该试试这个
UPDATE foo SET x ="", y = "", z = "" WHERE x is NULL and y is NULL and z is NULL UPDATE foo SET x =“”,y =“”,z =“”其中x为NULL且y为NULL且z为NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.