我使用 sqlite 并有一个看起来像的表:
我正在尝试使用来自 col2 对应的 col1 的值更新引用列
我试过查询
update tab1
set refer = (select col2 from tab1 where col1 = refer)
where col1 = 2
这但不起作用。
我也试过
update tab1
set refer = (select tem1.col2
from tab1 tem1, tab1 tem2
where tem1.col1 = tem2.refer and tem2.col1=2)
where col1 = 2
这有效。
但我不确定这是否是正确的做法。
查看您的代码似乎是您需要的
update tab1
set refer = col2
when col1 = refer
and col1 = 2
那意味着
update tab1
set refer = col2
when refer 2
oherwise fi yoru 正在使用子查询在同一个表上寻找更新,您应该使用内部联接
在 mysql
update tab1
INNER JOIN (
select col1, col2
from tab1
where col1 = refer ) t t.col1 = tabl1.col1 and col1 = 2
在 sqllite 中你可以使用
update tab1
set refer = (select t.col2 from (
select col2 from tab1 where col1 = refer
) t )
where col1 = 2
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句