【问题标题】:SQL update on one table checking condition from another table从另一个表检查一个表的 SQL 更新条件
【发布时间】:2019-11-13 08:20:37
【问题描述】:

我正在使用以下语句查询一列:

SELECT t1.col1 from table1 t1, table2 t2  
WHERE t1.col1 = t2.col2 and t2.col3 IN (data1, data2);

我正在尝试更新满足上述 where 条件的 t1.col1,但出现错误。

这是我尝试过的更新语句:

update t1 set t1.col1 = 1 from table1 t1 INNER JOIN table2 t2 where t2.col3 IN ( data1, data2 );

如果列出的数据与 t2.col3 中的数据匹配,我想更新 t1.col1 的值

【问题讨论】:

标签: database db2 sql-update inner-join


【解决方案1】:

您应该可以在这里使用相关子查询语法:

UPDATE table1 t1
SET col1 = 1
WHERE EXISTS (SELECT 1 FROM table2 t2 WHERE t2.col2 = t1.col1 AND t2.col3 IN (data1, data2));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2013-02-15
    • 2022-01-23
    相关资源
    最近更新 更多