SQLITE 更新带条件…case then?

huangapple go评论85阅读模式
英文:

SQLITE Update with conditions....case then?

问题

在此先感谢。我正在尝试使用另一个表中的日期更新表格。我已经成功完成了这一步,但现在我想仅在原始表格中的四个条件均为真时才更新每一行。

UPDATE newtable SET Serial = CASE WHEN (
     (SELECT ETI FROM oldtable) != 0.0) 
     AND ((SELECT Serial FROM oldtable) != 0000) 
     AND ((SELECT Hardware FROM oldtable) != 0000) 
     AND ((SELECT Software FROM oldtable) != 0000)
    THEN (SELECT Serial FROM oldtable WHERE newtable.IDENT = oldtable.IDENT)
END,

然而,此更新会影响所有行,因此这四个条件似乎不起作用。

感谢您的时间。

英文:

Thanks in advance.
I'm trying update a table with date of another table. I got this correctly, but now i want update each row only if 4 conditions are true in the original table.

UPDATE newtable SET Serial = CASE WHEN(
 (SELECT ETI FROM oldtable) != 0.0) 
 AND ((SELECT Serial FROM oldtable) != 0000) 
 AND ((SELECT Hardware FROM oldtable) != 0000) 
 AND ((SELECT Software FROM oldtable) != 0000)
THEN (SELECT Serial FROM oldtable  WHERE newtable.IDENT= oldtable.IDENT)END,

....

this update all the row, so the 4 conditions don't work.

Thanks for you time.

答案1

得分: 0

我认为你可以使用相关子查询来实现这个,就像这样:

update newtable
set serial = (
    select serial 
    from oldtable
    where ident = newtable.ident and serial <> 0 and hardware <> 0 and software <> 0 and eti <> 0
)
where exists (
    select serial 
    from oldtable
    where ident = newtable.ident and serial <> 0 and hardware <> 0 and software <> 0 and eti <> 0
)

很不清楚列 serialhardwaresoftware 等的数据类型是什么。我假设它们是数字(因为它们看起来像数字)。你可能需要根据你的确切用例进行适当的调整。

英文:

I think that you can do this with correlated subqueries, like so:

update newtable
set serial = (
	select serial 
	from oldtable
	where ident = newtable.ident and serial &lt;&gt; 0 and hardware &lt;&gt; 0 and software &lt;&gt; 0 and eti &lt;&gt; 0
)
where exists (
	select serial 
	from oldtable
	where ident = newtable.ident and serial &lt;&gt; 0 and hardware &lt;&gt; 0 and software &lt;&gt; 0 and eti &lt;&gt; 0
)

It is quite unclear what the datatypes of columns serial, hardware, software and so on is. I assumed they are numbers (because the look like numbers). You might need to adapt this to your exact use case.

huangapple
  • 本文由 发表于 2020年4月6日 17:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61056523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定