Database 仅当值不为空时,SQLite才更新列

Database 仅当值不为空时,SQLite才更新列,database,sqlite,Database,Sqlite,查询:更新项目表集合field1=field1\u西班牙语,field2=field2\u西班牙语 问题:只有在字段1\u西班牙语不为空时,我才能使用字段1\u西班牙语更新字段1?如果field2\u西班牙语不为空,我想用field2\u西班牙语更新field2 谢谢 coalesce()函数将返回传递给它的第一个非空参数。因此,在本例中,由于field2_西班牙语为null,它将field2设置为field2(基本上不做任何操作) 要支持空字符串和空值,请尝试以下操作: 假设所有这些列都在

查询:
更新项目表集合field1=field1\u西班牙语,field2=field2\u西班牙语

问题:只有在
字段1\u西班牙语
不为空时,我才能使用
字段1\u西班牙语
更新
字段1
?如果
field2\u西班牙语
不为空,我想用
field2\u西班牙语
更新
field2

谢谢

coalesce()
函数将返回传递给它的第一个非空参数。因此,在本例中,由于field2_西班牙语为null,它将field2设置为field2(基本上不做任何操作)

要支持空字符串和空值,请尝试以下操作:


假设所有这些列都在同一个表中:

update some_table
set field1=field1_spanish,
field2=field2_spanish
where field1_spanish is not null
and field2_spanish is not null;
如果
field1
field2
表中
,而
*\u西班牙语
列在
表中
,那么……那么,您必须执行相关子查询。假设
table
有一个
id
主键,该主键由
table\u西班牙语
引用,您可以执行以下操作:

update table a
set field1=(select s.field1_spanish 
    from table_spanish s 
    where field1_spanish is not null
    and s.id=a.id),
field2=(select s.field2_spanish 
    from table_spanish s 
    where field2_spanish is not null
    and s.id=a.id);
或者,您可以通过联接填充暂存表,然后从
表中删除相关条目
,并从暂存表中插入新数据(确保所有这些都使用事务!)


对于第二种方法。

BTW-sqlfiddle.com是我的网站非常棒的网站和答案!谢谢@JakeFeasel-优秀网站!我真希望它不会劫持我的后退按钮,但是…@JackManey它并没有真的劫持你的后退按钮,而是在你运行查询时只会在你的浏览历史中添加更多的状态。点击“back”键足够多,在你完成之前的工作后,你最终应该离开这个网站。空字符串和null不一样。相反,您必须执行以下操作:
update some_table
set field1=field1_spanish,
field2=field2_spanish
where field1_spanish is not null
and field2_spanish is not null;
update table a
set field1=(select s.field1_spanish 
    from table_spanish s 
    where field1_spanish is not null
    and s.id=a.id),
field2=(select s.field2_spanish 
    from table_spanish s 
    where field2_spanish is not null
    and s.id=a.id);