简体   繁体   English

如何替换多列中存在的值?

[英]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: 这将更新所有行,其中xyz列中的任何一个具有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.

相关问题 如何为单个列中存在的值添加多个条件并将结果显示在单独的列中? - How to add multiple conditions for the values present in a single column and display the result in separate columns? 如何运行一个MYSQL INSERT查询,该查询不基于存在自动增量ID的多个列替换重复项? - How to run a MYSQL INSERT query that doesn't replace duplicates based on multiple columns where auto-increment id is present? 如何检查两个列中是否存在两个值? - How to check two values are present in two columns or not in mysql? 如何对多个表中存在的相同列求和并将结果显示为一个 - How to sum same columns present in multiple tables and show there result as one 如何选择另一个表中多列的ID? - How to select the ids present in another table multiple columns? 如何比较具有多个值的多个表列? - How to compare multiple table columns with multiple values? 如何从不存在于多个表中的表中获取不同的值 - How to get distinct values from a Table not present in multiple table 如何检查带有“ nid”列的表中的多个值,并删除它们(如果存在) - How to Check a Table with column “nid” for multiple values and delete them if they are present 如何用 SQL 替换单个列中的多个值? - How to replace multiple values in a single column with SQL? 如何使用REPLACE()替换mysql SELECT查询中的1列中的多个值? - How to replace multiple values in 1 column in mysql SELECT query using REPLACE()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM