gpt4 book ai didi

sqlite - SQLite-更新命令产生空值

转载 作者:行者123 更新时间:2023-12-03 19:44:46 27 4
gpt4 key购买 nike

我无法在SQLite数据库上执行一些更新。我正在Windows上使用SQLite 3 Shell。
我正在运行以下命令:

update resovled_chrom_counts set genus =  
case resolved_name_full
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;


它似乎适用于大多数行,但是有些行在其属字段中仅以空值结尾。我尝试使用此表的“ id”字段手动检查其中的一些。例如,我发现id ='kew-1'的行在它的属字段中为null,然后运行以下查询:

select substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)  
from resovled_chrom_counts
where id='kew-1';


令我惊讶的是,我得到了一个结果(不是null)!
看起来该查询在'select'语句下有效,但在'update'语句下无效。
谁能提供解释和/或解决方案?
任何帮助,将不胜感激。谢谢!

最佳答案

问题不在于substr(resolved_name_full...,而在于CASE。

CASE expression可以具有两种不同的形式:


CASE x WHEN y THEN ...:将x的值与y的值进行比较。
CASE WHEN a THEN ...:这检查a的值是true还是false。


UPDATE语句中的问题是在CASE之后直接有一个值(resolved_name_full),因此将resolved_name_full的值与表达式resolved_name_full is not null and resolved_name_full != ''的值进行比较,并且此比较始终失败,因为resolved_name_full永远不会碰巧是01

只需使用CASE表达式的第二种形式:

update resovled_chrom_counts set genus =
case
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;


SQLFiddle

关于sqlite - SQLite-更新命令产生空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23102840/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com