gpt4 book ai didi

mysql - 使用子查询使用同一个表中的值更新表

转载 作者:行者123 更新时间:2023-11-30 21:45:51 24 4
gpt4 key购买 nike

我使用 sqlite 并有一个如下所示的表:

my table

我正在尝试使用 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

这有效。

但我不确定这是否是正确的做法。

Expected

最佳答案

查看您的代码似乎是您需要的

   update tab1 
set refer = col2
when col1 = refer
and col1 = 2

意思是

   update tab1 
set refer = col2
when refer 2

否则 fi yoru 正在使用子查询在同一个表上查找更新,您应该可以使用内部联接

在数据库中

 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

关于mysql - 使用子查询使用同一个表中的值更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49530332/

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